How can I access the previous/next element in an ArrayList?

后端 未结 5 584

I iterate through an ArrayList this way:

for (T t : list){
  ...
}

When I did this, I never thought I had to access the previous and next eleme

5条回答
  •  迷失自我
    2021-02-04 09:20

    You can access any element in ArrayList by using the method get(index).

    import java.util.ArrayList;
    
    
    public class Test {
        public static void main(String[] args) {
            ArrayList array = new ArrayList();
    
            array.add(1);
            array.add(2);
            array.add(3);
    
            for(int i=1; i

提交回复
热议问题