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 › Variables of objects from ArrayList
Page Index Toggle Pages: 1
Variables of objects from ArrayList (Read 568 times)
Variables of objects from ArrayList
Feb 7th, 2009, 5:19am
 
  Hello guys! Could someone help who has some exp about working with objects from ArrayList. Please tell where can read about:
 1) How to define object's index in ArrayList?
    (When using loop: for(int i; i<list.size()...)
 2) How to work with objects' inner variables, when
    objects placed in ArrayLists?
 3) How to communicate 2 diff objects' variables from 2    
    diff ArrayLists?

I've found interesting topic for me:

http://forums.sun.com/thread.jspa?forumID=31&threadID=5193767


Citation:

" Are you trying to do 1 or 2?
1. Given a bunch of records, look up tom's in particular and do something with it.
or
2. Given a bunch of records, go through them all and do something with each record in turn."

Unfortunally 2nd question descripted there, when I need 1st.


And the last question: What is better for me from there:


for(TaxRecord record : records) {
   int income = record.getIncome();
   //...
}

for(Iterator <TaxRecord > i = records.iterator(); i.hasNext(); ) {
   TaxRecord record = i.next();
   int income = record.getIncome();
   //...
}

for(int j = 0; j < records.size(); ++j) {
   TaxRecord record = records.get(j);
   int income = record.getIncome();
   //...
}
Re: Variables of objects from ArrayList
Reply #1 - Feb 7th, 2009, 11:40am
 
Warning! The new syntax (for (i : I), I<C>) isn't supported in Processing, unless you use it in .java files.

I am not sure to understand 1), but there is a add(int index, E element) method in ArrayList. Unlike a pure array, this add pushes up the previous elements and you cannot add at arbitrary places, only within the size().
Example:
Code:
ArrayList al = new ArrayList(10);

// If I don't put this line, the add below will throw an error
for (int i = 0; i < 10; i++) al.add(">--" + i);

al.add(1, "One");
al.add(3, "Three");
al.add(5, "Five");
al.add(8, "Eight");

ListIterator it = al.listIterator();
while (it.hasNext())
{
int i = it.nextIndex();
String s = (String) it.next();
println(i + " " + s);
}


2) In old syntax, when you do a get() (or a next()), you have a generic Object. You have to cast (like above) the object to its real type to be able to access the inner variables (fields of the class).

3) I am not sure to understand the question.
Re: Variables of objects from ArrayList
Reply #2 - Feb 7th, 2009, 5:28pm
 
   Thank you PhiLho.
About my main question: For example I have 2 different undefined ArrayLists, one of them holds circles and other boxes - this objects created form their classes. But each object brings it's undefined color and and it's height - this variables are inner for class created from. When circle stamps to the box - how to make circle get color and height of current box? How circle can get current index of box?
 When I work I use everywhere "for(int i; i<AList.size();..."

Maybe everything have to work - maybe I'm just "parked in the rong place"?
Re: Variables of objects from ArrayList
Reply #3 - Feb 7th, 2009, 6:35pm
 
Within the loop, you get the nth object with:

Box b = (Box) listOBoxes.get(n);

and same for circles.
Then you can access any data within the object(s), as usual.
Re: Variables of objects from ArrayList
Reply #4 - Feb 7th, 2009, 10:40pm
 
 I got it, thank you, I just was stumped... Smiley
Re: Variables of objects from ArrayList
Reply #5 - Feb 8th, 2009, 3:20am
 
  I've solved that...
But may I ask you again? It's intresting topic for me...
I made 2 loops inside class Circle:

1st within  mouseDragged { ( for int n;...) { Box b = (Box) listOBoxes.get(n);...

2nd within   mouseReleased { ( for int n;...) { Box b = (Box) listOBoxes.get(n);...

Index of boxes is missed and I can't catch up how to make 1st and 2nd loop to collaborate when there are 2 loops. Should it work in further?

Will this be a good idea to create new void mouseAct() { dragged, pressed, etc.) for using 1 loop there?
Re: Variables of objects from ArrayList
Reply #6 - Feb 8th, 2009, 4:55pm
 
Quote:
Index of boxes is missed and I can't catch up how to make 1st and 2nd loop to collaborate when there are 2 loops.

I fear I don't get it. What do you mean "index is missed"? And what kind of collaboration?

But in general, yes, if you do twice the same thing, it is a good idea to make a more general function and call it where needed.
Re: Variables of objects from ArrayList
Reply #7 - Feb 11th, 2009, 6:25am
 
  I got it. Thank you.
Page Index Toggle Pages: 1