We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Array Crash Course - Another Query
Page Index Toggle Pages: 1
Array Crash Course - Another Query (Read 1884 times)
Array Crash Course - Another Query
May 29th, 2010, 6:24am
 
Hey,

I'm needing a fast crash course on arrays....

I have an array with an index and a value. I know the value. How do I pull out the index??? What if it's a double array with two indexes? How do I pull them each out separately based on the value???

If I can't do this is there another, simpler, way to store three pieces of information so that they are kept together, each to be pulled out and used separately.

Thanks,
IceKat.
Re: Array Crash Course - Another Query
Reply #1 - May 29th, 2010, 7:24am
 
Check your value against the values in the array and save the index of the one(s) where it is equal. In it's simplest form this would be something like the example below. Which returns the index of the last occurrence of the value or -1 in case it's not found at all. Kind of like lastIndexOf for an ArrayList.

Code:
int[] numbers = new int[5];
int index = -1;
int value = 90;

void setup() {
 size(300,300);
 background(255);
 numbers[0] = 90;
 numbers[1] = 150;
 numbers[2] = 30;
 numbers[3] = 10;
 numbers[4] = 80;
 
 for (int i=0; i < numbers.length; i++) {
   if (numbers[i] == value) {index = i;}
 }

 println(index);
}

Object-oriented programming can also be useful for grouping information. Create a class and from this multiple instances (the individual objects). That way you can have a lot of objects with their own data, for example xyz-location, speed, colors etcetera. All information is grouped together in the object.
Re: Array Crash Course - Another Query
Reply #2 - May 29th, 2010, 8:26am
 
Good advices above...
Classes might seem intimidating but you can use them in their simplest form, a way to group together a number of variables (a name, some booleans, integers and float numbers, etc.).

Side note:
when I do a search loop like this one, even a short one as shown, I always add a break; after the index assignment, as there is no reasons to continue the search... Smiley
Re: Array Crash Course - Another Query
Reply #3 - May 29th, 2010, 8:30am
 
I'd second the use of a Class rather than multi-dimensional arrays.  The pros/cons have been discussed before on this forum and classes usually come out on top...  Certainly far simpler to work with and you have the advantage of being able to build in helper methods etc.

As for your question - you need to iterate through the array elements (or objects) and check whether the value is equal; then return the index.  However you should consider whether all values within your array/or objects are unique and how to handle duplicates...
Re: Array Crash Course - Another Query
Reply #4 - May 30th, 2010, 8:55am
 
PhiLho  wrote on May 29th, 2010, 8:26am:
Good advices above...
Classes might seem intimidating but you can use them in their simplest form, a way to group together a number of variables (a name, some booleans, integers and float numbers, etc.).

Side note:
when I do a search loop like this one, even a short one as shown, I always add a break; after the index assignment, as there is no reasons to continue the search... Smiley


Indeed, good advices. I've posted these before, but maybe it's handy to have some code examples. Below are part of my custom 'standard' functions and classes, but they may very well help you. I've been through the same process when loading data; I used to save the columns to separate arrays, but classes are the way to store data. And these are in fact very logical when you use them.

Code:
int returnIndexOf(String[] arr, String var) {

 for (int i = 0; i < arr.length; i++) {
   if (arr[i].equals(var)) {
return i;
   }
 }
 return -1;
}

int returnIndexOf(int[] arr, int var) {

 for (int i = 0; i < arr.length; i++) {
   if (arr[i] == var) {
return i;
   }
 }
 return -1;
}

etc.


and the "Point class", one of the simplest examples of using classes to store data, instead of separate xpos and ypos arrays (or even twodimensional coordinate arrays)

Code:
class Point {

 float x, y, z;

 Point(float x, float y) {

   this.x = x;
   this.y = y;

 }

 Point(float x, float y, float z) {

   this.x = x;
   this.y = y;
   this.z = z;

 }

}


And example usage:

Code:
Point[] coordinates = new Point[0];

void setup() {

 size(500, 500);
 frameRate(30);

}

void draw() {

 background(255);

 noFill();
 beginShape();
 for(int i = 0; i < coordinates.length; i++) {
   vertex(coordinates[i].x, coordinates[i].y);
 }
 endShape();

}

void mouseMoved() {

 Point mousepos = new Point(mouseX, mouseY);
 coordinates = (Point[]) append(coordinates, mousepos);

}



Page Index Toggle Pages: 1