0

This is a section of my code, I have an ArrayList of 10 objects called "bob" and I want to loop through them so that each of their names (a local integer defined in the bob class) to be put in the array named "names" in order.

for (bob b : bob) {      
    for (int i = 0; i < 10; i++){
        names[i] = b.name;
    }
}

I tried this approach:

for (bob b : bob) {      
    for (int i = 0; i < 10; i++){
        names[i] = b[i].name; //I added the "[i]" after b attempting to loop through
                              //the arraylist but it does not work
    }
}

the syntax does not seem to allow me to loop through the arraylist of the objects like that. I am a beginning programmer so please excuse my lack of programming knowledge. It would be very helpful if someone could at least give me an idea of where to go from here. Thank you in advance!

2 Answers 2

1

When dealing with ArrayList you need to use the set() and get() methods to access the contents of it. Here's a somewhat hamfisted attempt at recreating the scenario you describe. Hope it helps.

class Bob { 
  int name; 
  Bob() {  
    this.name = floor(random(10000)); 
  } 
}

void setup(){
  ArrayList<Bob> alb = new ArrayList<Bob>();

  for(int i = 0; i < 50; i++){ //populate ArrayList
    alb.add(new Bob());
  }

  int[] names = new int[10];

  for(int i = 0; i < names.length; i++){
    names[i] = alb.get(i).name;        // use get() method
  }

  for(int i = 0; i < names.length; i++){
    print(names[i]);
    print('\n');
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your question highlights two techniques for iterating over a collection: with or without, an index. Each is best suited for different data structures and scenarios. It takes some experience to decide when to use one or the other, and is also a matter of personal style.

It is common to write code like for( int x: myInts ) and then realize you want the index of the current item, which isn't available. Or conversely, to write code like for( int i=first; i<last; i++) and then become irritated because determining first and last is tedious, or prone to bugs.

Notice your code is a double-nested loop. It says "iterate over each item in the collection Bob, and then for each one, iterate over each item in the collection of names". So if Bob had ten items, this would iterate one hundred total times, probably not what you want. You need to rewrite as a single, non-nested for loop ...

If you decide to iterate without an index, then names should be some type of list, where you can add items using append(). Consider the StringList available in Processing. Otherwise if you decide to iterate with an index, then names could be an array, although it could still be a list if it was already populated with old values which you wish to overwrite. The following shows both techniques:

void setup()
{
  ArrayList<String> baseList = new ArrayList<String>(10);
  for( int i=0; i<10; i++ )
    baseList.add( i, Integer.toString( i + (i*10) ) );

  // Approach 1: Iterate without an index,
  // build a list with no initial allocation and using append()
  StringList namesList = new StringList();
  for( String s : baseList )
  {
    namesList.append( s );
    println( namesList.get( namesList.size()-1 ) );
  }

  // Approach 2: Iterate with an index,
  // build a list using preallocation and array access
  String[] namesArray = new String[10];
  for( int i=0; i<10; i++ )
  {
    namesArray[i] = baseList.get(i);
    println( namesArray[i] );
  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.