Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  retrieving multiple values from an array  (Read 952 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2012-04-23 12:02:54 »

I've created an array that stores (x,y) values (an x and a y for each array index), how do I extract the x and y has a singular value

i.e. int x = Array.get("x value");

Any help would be awesome Smiley also sorry for the noob question!
Offline westloar

Junior Member





« Reply #1 - Posted 2012-04-23 12:32:46 »

I think I've solved it, no idea why I didn't think to use a 2 dimensional array!
Offline jonjava

JGO Knight


Medals: 32



« Reply #2 - Posted 2012-04-23 12:42:29 »

:)

You could also make a new class that holds the x and y values and have an ArrayList of those.

Ex:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Point {
 public int x = 0;
 public int y = 0;
 
 // Constructor
public Point(int x, int y) {
  this.x = x;
  this.y = y;
 }
}


And then use them like f.ex:

1  
2  
3  
4  
5  
6  
7  
public static void main(String[] argv) {
 ArrayList<Point> list = new ArrayList<Point>();
 
 Point p = new Point(5, 5);
 p.x = 10;
 list.add(p);
}

Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline westloar

Junior Member





« Reply #3 - Posted 2012-04-23 12:53:50 »

Am I right in thinking this code would print out "2,1" :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
private Array boomPath [x] [y];

for(i < 10; i++) {

   boomPath.add(i*2,i);
}

int x = boomPath.get(1,1);
int y = boomPath.get(1,2);

system.out.println(x + "," + y)
Offline Danny02

JGO Knight


Medals: 36



« Reply #4 - Posted 2012-04-23 13:02:52 »

I don't even ....



just do
1  
2  
3  
4  
5  
Point[] points = new Point[10];
points[0] = new Point(2,5);
...
Point aPoint = points[0];
System.out.println("x: "+aPoint .getX()+", y: "+aPoint.getY())


btw, when you are the boomerang guy, don't calculate path positions before throwing this pice of wood.
Just calculate the next position while updateing all other objects in your game loop
Offline westloar

Junior Member





« Reply #5 - Posted 2012-04-23 13:16:09 »

hey this was posted in the newbie section for a reason!

But yes in the light of the point object this question does look rather silly XD

out of interest would my code print what I said it would?
Offline Danny02

JGO Knight


Medals: 36



« Reply #6 - Posted 2012-04-23 14:06:56 »

when you mean with an array an standart array

one can do it like this

1  
2  
3  
4  
int[][] points = new int[POINT_COUNT];

for(int i = 0; i < POINT_COUNT; ++i)
  points[i] = new int[]{i*2, i};


other way to do it "saver"^^
1  
2  
3  
4  
5  
6  
7  
int[][] points = new int[POINT_COUNT][2];

for(int i = 0; i < POINT_COUNT; ++i)
{
  points[i][0] = i*2; //x value
 points[i][1] = i; //y value
}
Offline jonjava

JGO Knight


Medals: 32



« Reply #7 - Posted 2012-04-23 14:14:08 »

I'm not too familiar with the "Array" in java. But if you don't need an ArrayList, why not make an array of ints?

Ex:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
private int[] array = new int[10]; // Array of ints that can hold 10 ints from 0 to 9
private int[][] array2 = new int[5][5]; // 2d array

for(int i=0; i < array.size(); i++) {
 array[i] = i*i;
}

int num1 = array[1];
int num2 = array[2];

system.out.println(num1 + "," + num2)


But once allocated arrays can't change in size. That's why you can use the ArrayList class which changes its size automatically for you if needed. And it also comes with a lot of helpful methods for handling the data inside.

The "<>" inbetween the ArrayList is what's called a "Generic" iirc, it just tells the compiler what kind of Objects you intend on using the ArrayList for so that if you by mistake put something else in there it will notify you.

Of course, you can use the Object as the generic type to make it store ANY kinds of objects. You can then internally check what kind of objects it holds with the "instanceof" keyword.

Ex:

1  
2  
3  
4  
5  
6  
7  
8  
9  
ArrayList<Object> list = new ArrayList<Object>(10); // Initial size set to 10, it will increase automatically when needed.

list.add( new String("Hello") );
list.add( new Integer(3) );

for (int i=0; i < list.size(); i++) {
 Object data = list.get(i);
 if (data instanceof String) System.out.println( data );
}


Please note though that the above method of using instanceof in its current state isn't really recommended, just as an example to demystifying the generic <> tag :P

Quote from: Effective C++, by Scott Meyers
"Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself."

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (73 views)
2013-05-17 12:29:12

alaslipknot (85 views)
2013-05-16 12:24:48

gouessej (114 views)
2013-05-15 15:53:38

gouessej (109 views)
2013-05-15 15:17:58

theagentd (120 views)
2013-05-15 06:01:13

theagentd (109 views)
2013-05-15 06:00:54

StreetDoggy (154 views)
2013-05-14 06:56:26

kutucuk (176 views)
2013-05-12 08:10:36

kutucuk (173 views)
2013-05-12 06:36:09

UnluckyDevil (182 views)
2013-05-11 20:09:57
Complex number cookbook
by Roquen
2013-04-24 03:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 07:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 07:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 07:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 08:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 07:17:38

Java Data structures
by Roquen
2013-03-29 04:21:12

Topic Request
by kutucuk
2013-03-22 12:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.141 seconds with 20 queries.