Home > front end >  Copying list to another list and adding static value in java
Copying list to another list and adding static value in java

Time:11-12

I have list 'abc'contains elements. I want to have a new list 'cde' in which first element is '123' and then has to add all elements of abc .How to achieve this.I am beginner to coding

 private List<object> abc;

 private List<object> cde;

cde={123 ,//elements of abc list}

CodePudding user response:

Something along the following lines should do it:

List<Integer> abc = new ArrayList<>();
List<Integer> cde = new ArrayList<>();

cde.add(123); // Adds 123 to your list
cde.addAll(abc); // Adds abc content at the end of list cde

You would need to change cde.add(123); to cde.add(0, 123); if your cde list is not empty and you want to place 123 as its first element.

CodePudding user response:

[Edit: wrong use of static method Collections.addAll(...) removed]

  • Related