Processing 循环遍历对象变量的arraylist,并在处理过程中将其输入到数组中

Processing 循环遍历对象变量的arraylist,并在处理过程中将其输入到数组中,processing,Processing,这是我代码的一部分,我有一个名为“bob”的10个对象的ArrayList,我想循环遍历它们,以便将它们的每个名称(bob类中定义的局部整数)按顺序放入名为“names”的数组中 for (bob b : bob) { for (int i = 0; i < 10; i++){ names[i] = b.name; } } for(bob:bob){ 对于(int i=0;i

这是我代码的一部分,我有一个名为“bob”的10个对象的ArrayList,我想循环遍历它们,以便将它们的每个名称(bob类中定义的局部整数)按顺序放入名为“names”的数组中

for (bob b : bob) {      
    for (int i = 0; i < 10; i++){
        names[i] = b.name;
    }
}
for(bob:bob){
对于(int i=0;i<10;i++){
名称[i]=b.名称;
}
}
我尝试过这种方法:

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
    }
}
for(bob:bob){
对于(int i=0;i<10;i++){
names[i]=b[i].name;//我在b尝试循环之后添加了“[i]”
//arraylist,但它不起作用
}
}
语法似乎不允许我像那样循环遍历对象的arraylist。我是一个初级程序员,所以请原谅我缺乏编程知识。如果有人能给我一个从这里到哪里的想法,那将是非常有帮助的。提前谢谢你

处理时,需要使用set()get()方法来访问它的内容。下面是一个有点笨拙的尝试,试图重新创建您描述的场景。希望能有帮助

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');
  }
}
类Bob{
int名称;
Bob(){
this.name=楼层(随机(10000));
} 
}
无效设置(){
ArrayList alb=新的ArrayList();
对于(int i=0;i<50;i++){//populate ArrayList
alb.add(新的Bob());
}
int[]名称=新int[10];
for(int i=0;i
您的问题强调了在集合上迭代的两种技术:带索引或不带索引。每种方法都最适合不同的数据结构和场景。这需要一些经验来决定何时使用其中一种,也是个人风格的问题

编写类似于(intx:myInts)的代码,然后意识到需要当前项的索引,而当前项的索引不可用,这是很常见的。或者相反,编写类似于(inti=first;i)的
的代码
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] );
  }

}