简体   繁体   中英

How To Sort ArrayList in Different List

import java.util.ArrayList;
import java.util.Arrays;

public class MyData {
    public static void main(String args[])
    {
        ArrayList<String> arrayList = new ArrayList<String>(
            Arrays.asList("1","2","3","4","5","6","7","8")
        );

        System.out.println("Size of AyyayList= "+arrayList.size());
        for(int i=0;i<arrayList.size()/2;i++)
        {
            System.out.println(i+" "+(i+1));
        }
    }
}

my Output 
  Size of AyyayList= 8
  0 1
  1 2
  2 3
  3 4

Need Output

 Size of AyyayList= 8
 0 1
 2 3
 4 5
 6 7

In that case you need :

for(int i=0;i<arrayList.size()/2;i++)
{
    System.out.println(2*i+" "+(2*i+1));
}

在for循环中,而不是i++编写i = i + 2.问题是,您只将i加1

Try this way,hope this will help you to solve your problem.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM