retrieving multiple values from an array

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 :slight_smile: also sorry for the noob question!

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

:slight_smile:

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

Ex:

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:

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

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


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)

I don’t even …

just do


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

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?

when you mean with an array an standart array

one can do it like this


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”^^


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
}

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:

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:


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 :stuck_out_tongue:

[quote=“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.”
[/quote]