【问题标题】:Interation of inner arraylist of arraylist of arraylistsarraylist的arraylist的内部arraylist的交互
【发布时间】:2015-05-17 22:33:58
【问题描述】:

我正在尝试制作数组列表的数组列表。我的代码到处都是,我只是在添加东西并尝试错误尝试。最后我想做的是,说我有一辆车......它有速度,英里,mpg,如果他们想买它,把它添加到arraylists的arraylist中,然后就能得到一个拥有汽车的清单,只是为了让您了解我正在尝试做的事情。 idk 如果有帮助,或者也许有更好的方法来做到这一点。我只需要内部数组迭代的帮助,其余的我可以弄清楚。现在它只是迭代主数组列表并显示其中的所有内容看起来像一个数组。我宁愿使用 for 循环然后迭代类,但它的工作方式对我有好处。

public class Thelist {

    String a ="aa";
    String b ="bb";
    String c ="cc";

    static ArrayList<ArrayList<String>> collection = new ArrayList<ArrayList<String>>();
    static int count=0;
    ArrayList<String> listOfSomething1 = new ArrayList<String>();

    public void gg(){

    ArrayList<String> listOfSomething1 = new ArrayList<String>();
    listOfSomething1.add(a);
    listOfSomething1.add(b);
    listOfSomething1.add(c);

    collection.add(listOfSomething1);    
    count++;    
    }

    public void jk(int u){

    //this one feel like i can make new arraylist and use for loop for the collection size and iterate each one
    //but seems pretty hacky to me

        ArrayList<String> lis = new ArrayList<String>();
        lis.addAll(collection.get(u));

        for(int i=0;i<lis.size();i++ ){
          System.out.println("each "+i+" "+lis.get(i));
        }       
    }

    public void ll(){
        System.out.println(collection.size());
        System.out.println(collection.get(0).size());
    }

    public void ccc(String x,String y, String z){
        this.a= x;
        this.b=y;
        this.c=z;
    }

    public void remo(int r){

        collection.remove(0);
        count--;
    }

    public void getcount(){

        System.out.println("the count "+count); 

    }

    public void showall(){

        //this one shows all of the arraylist as arrays it looks like to me, I want each element
        // feel like i should be able to add another for loop to iterate over listOfSomething1
        //which is be add to the collection arraylist
        // but it don't work tryed for each too
        for(int i=0;i < collection.size();i++){
            System.out.println("uoo "+collection.get(i));

        }
    }
}






public class MainActivity extends Activity {

static int oo = 1;

Thelist ll = new Thelist();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   

}

public void woow(View v){

     String gg = Integer.toString(oo);
     ll.ccc("phi"+gg, "phil"+gg, "phily"+gg);   
     ll.gg();       
     oo++;
}


public void shoo(View v){

ll.showall();   
}

}

【问题讨论】:

  • 查看您的gg() 方法:您声明一个新的ArrayList 并添加到它而不是字段。没有我在哪里看到你添加到字段listOfSomething1,这可能就是为什么迭代它会导致任何结果
  • 请正确命名您的方法和变量。它可以帮助人们看到您想要实现的目标,从而使他们能够为您提供更好的帮助。
  • @Vince Emigh 抱歉,我是从我的主班添加的,只是把 Thelist 放了,抱歉,我会添加一个编辑,让其他人都清楚
  • @mike 我知道只是让它进入工作模式,我一直在左右改变它,只使用最快的,抱歉!

标签: java arraylist


【解决方案1】:

只需使用两个嵌套的 for 循环。第一个迭代集合,每次迭代产生一个ArrayList&lt;String&gt;。获取此列表并在第二次迭代中对其进行迭代。为简单起见,您可以在此处使用 for-each 循环:

for(ArrayList<String> cars : collection) {
    for(String car : cars) {
        System.out.println(car);
    }
}

【讨论】:

  • 很抱歉,奥马尔的回答是,尽管它们几乎相同,只是我对设置的偏好......但非常感谢,我很感激!
  • for each 时代,使用索引迭代循环实际上被认为是不好的风格。对于来自底层实现的每个抽象,并产生可能的最佳性能,例如与ArrayList(恒定时间复杂度)相比,LinkedList(线性时间复杂度)上的get 操作要慢得多。
【解决方案2】:
   public void showall(){

        for(int i=0;i < collection.size();i++)
            for(int j=0; j< collection.get(i).size();j++)
              System.out.println(collection.get(i).get(j));

        }

【讨论】:

  • 这正是我要找的……是的,我看到你漏掉了; j++ 很好,我明白了,我很快就会排除你的答案,只是给大家几分钟,以防他们写下答案,但这正是我要找的东西!!!
猜你喜欢
  • 2012-11-27
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多