Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 1838 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2012-04-23 10: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 10:32:46 »

I think I've solved it, no idea why I didn't think to use a 2 dimensional array!
Offline jonjava
« Reply #2 - Posted 2012-04-23 10: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!
Legends of Yore - The Casual Retro Roguelike
Offline westloar

Junior Member





« Reply #3 - Posted 2012-04-23 10: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
« Reply #4 - Posted 2012-04-23 11: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 11: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
« Reply #6 - Posted 2012-04-23 12: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
« Reply #7 - Posted 2012-04-23 12: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.

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (72 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (43 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!