Skip to main content
Formatting. Whitespace in code.
Source Link
Lii
  • 12.1k
  • 9
  • 69
  • 92

Basically there are three ways without iterating manually,

1 Using constructor

ArrayList<Dog> dogs=getDogsdogs = getDogs();
ArrayList<Dog> clonedList=newclonedList = new ArrayList<Dog>(dogs);

2 Using addAll(Collection<? extends E> c)addAll(Collection<? extends E> c)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

3 Using addAll(int index,Collection<? extends E> c)addAll(int index, Collection<? extends E> c) method with intint parameter

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0, dogs);

NB : The behavior of these operations will be undefined if the specified collection is modified while the operation is in progress.

Basically there are three ways without iterating manually,

1 Using constructor

ArrayList<Dog> dogs=getDogs();
ArrayList<Dog> clonedList=new ArrayList<Dog>(dogs);

2 Using addAll(Collection<? extends E> c)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

3 Using addAll(int index,Collection<? extends E> c) method with int parameter

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0,dogs);

NB : The behavior of these operations will be undefined if the specified collection is modified while the operation is in progress.

Basically there are three ways without iterating manually,

1 Using constructor

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>(dogs);

2 Using addAll(Collection<? extends E> c)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

3 Using addAll(int index, Collection<? extends E> c) method with int parameter

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0, dogs);

NB : The behavior of these operations will be undefined if the specified collection is modified while the operation is in progress.

deleted 5 characters in body
Source Link
javatar
  • 1.3k
  • 1
  • 17
  • 24

Basically there are three ways without iterating manually,

1 Using constructor

ArrayList<Dog> dogs=getDogs();<br/>;
ArrayList<Dog> clonedList=new ArrayList<Dog>(dogs);

2 Using addAll(Collection<? extends E> c)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

3 Using addAll(int index,Collection<? extends E> c) method with int parameter

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0,dogs);

NB : The behavior of these operations will be undefined if the specified collection is modified while the operation is in progress.

Basically there are three ways without iterating manually,

1 Using constructor

ArrayList<Dog> dogs=getDogs();<br/>
ArrayList<Dog> clonedList=new ArrayList<Dog>(dogs);

2 Using addAll(Collection<? extends E> c)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

3 Using addAll(int index,Collection<? extends E> c) method with int parameter

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0,dogs);

NB : The behavior of these operations will be undefined if the specified collection is modified while the operation is in progress.

Basically there are three ways without iterating manually,

1 Using constructor

ArrayList<Dog> dogs=getDogs();
ArrayList<Dog> clonedList=new ArrayList<Dog>(dogs);

2 Using addAll(Collection<? extends E> c)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

3 Using addAll(int index,Collection<? extends E> c) method with int parameter

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0,dogs);

NB : The behavior of these operations will be undefined if the specified collection is modified while the operation is in progress.

added 176 characters in body
Source Link
javatar
  • 1.3k
  • 1
  • 17
  • 24

Why don't useBasically there are three ways without iterating manually,

1 Using constructor

ArrayList<Dog> dogs=getDogs();<br/>
ArrayList<Dog> clonedList=new ArrayList<Dog>(dogs);

2 Using addAll(Collection<? extends E> c) in ArrayList??

In your case:

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

FYI : You can use the3 Using addAll(int index,Collection<? extends E> c) method with int parameter too. ;)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0,dogs);

NB : The behavior of this operation isthese operations will be undefined if the specified collection is modified while the operation is in progress.

Why don't use addAll(Collection<? extends E> c) in ArrayList??

In your case:

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

FYI : You can use the method with int parameter too. ;)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0,dogs);

NB : The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Basically there are three ways without iterating manually,

1 Using constructor

ArrayList<Dog> dogs=getDogs();<br/>
ArrayList<Dog> clonedList=new ArrayList<Dog>(dogs);

2 Using addAll(Collection<? extends E> c)

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(dogs);

3 Using addAll(int index,Collection<? extends E> c) method with int parameter

ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = new ArrayList<Dog>();
clonedList.addAll(0,dogs);

NB : The behavior of these operations will be undefined if the specified collection is modified while the operation is in progress.

Source Link
javatar
  • 1.3k
  • 1
  • 17
  • 24
Loading