Luxy Luxy - 1 month ago 7
Java Question

Java : Fetch data from arraylist?

I have arraylist which has input data like

[1 2 3 4 5 7 8 9]
.

But the output should be
[7 8 9 1 2 3 4 5]
.

The last 3 values should come first. I dont know how do we do it in
ArrayList
. I think it is possible in
linkedlist
.

Below is my code.

import java.util.*;

public class iterate{

public static void main(String []args){
List<Integer> numbers = new ArrayList<Integer>();

for (Integer i : Arrays.asList(0,1,2,3,4,5,6,7))
numbers.add(i);
printList(numbers); // 0,1,2,3,4,5,6,7

}

}


This is what i know. Now how do i proceed

Answer

Try this:

List<Integer> array = Arrays.asList(1,2,3,4,5,7,8,9);
Collections.rotate(array, 3);
array.forEach(e -> System.out.print(e+" "));
Comments