簡體   English   中英

arraylists的arraylist的內部arraylist的交互

[英]Interation of inner arraylist of arraylist of arraylists

我正在嘗試制作arraylists的arraylist。 我的代碼無處不在,我一直在添加內容並嘗試錯誤嘗試。 最后,我想做的是,說我有一輛車……它有速度,英里,mpg,如果他們想購買它,請將其添加到arraylists的arraylist中,然后再獲得所擁有汽車的清單,只是為了讓您了解我正在嘗試做的事情。 idk是否有幫助,或者有更好的方法。 我只需要內部數組的迭代方面的幫助,其余的我都可以弄清楚。 現在,它只是迭代主arraylist並顯示其中的所有內容,看起來就像一個數組。 我寧願使用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();   
}

}

只需使用兩個嵌套的for循環即可。 第一個迭代集合,每次迭代產生一個ArrayList<String> 獲取此列表並在第二個迭代中對其進行迭代。 為了簡單起見,您可以在此處使用for-each循環:

for(ArrayList<String> cars : collection) {
    for(String car : cars) {
        System.out.println(car);
    }
}
   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));

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM