Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Janarthan S Sathiamurthy wrote:import java.util.Collections;
List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};
Collections.addAll(myList, myStringArray);
After this code, 'myList' should contain all the elements from the array.
Best regards,
Janarthan S
Enjoy life!
Jesper Young wrote:There are some important things to note with the solutions given above:
Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.
Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.
Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.
If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
Enjoy life!
"If the facts don't fit the theory, get new facts" --Albert Einstein
Jesper Young wrote:There are some important things to note with the solutions given above:
Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.
Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.
Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.
If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
http://www.thejavacode.com/
Jesper de Jong wrote:There are some important things to note with the solutions given above:
Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.
Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.
Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.
If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
Jesper de Jong wrote:There are some important things to note with the solutions given above:
Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.
Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.
Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.
If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
No Kaustubh No Fun, Know Kaustubh Know Fun..
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Kaustubh G Sharma wrote:
Jesper de Jong wrote:There are some important things to note with the solutions given above:
Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.
Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.
Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.
If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
Jesper you really rocks man...Wonderful explanation...keep up the good work![]()
Campbell Ritchie wrote:Welcome to the Ranch
Ming Wilson
Jesper de Jong wrote:There are some important things to note with the solutions given above:
Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.
Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.
Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.
If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
Regards,
Radhakrishna
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jesper de Jong wrote:There are some important things to note with the solutions given above:
Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.
Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.
Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.
If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
whatever can go wrong, will go wrong
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
C Bryant wrote:Yes well, that was sort of the point of the example. The code looks like it should work to me. It makes logical sense syntactically.
Why should there be a two step process? 1. Create the String array. 2. Convert the array to a list? Why can't I do this all in one statement?
C Bryant wrote:But, not the following?
C Bryant wrote:Sure, that's cool, and simpler, too. Thanks!
Only the second one doesn't compile?
How about these three?
01. Compiles, albeit with an "unchecked conversion" warning.
02. Does not compile.
03. Cut and pasted from the JavaDocs 1.6 API does not compile.
What gives? I want to assign to a List, because I do not want to write to implementation classes. I want to code to interfaces. Stooges, indeed!!
C Bryant wrote:
03. Cut and pasted from the JavaDocs 1.6 API does not compile.
Joanne
C Bryant wrote:Eclipse Indigo running Java 1.6 with Java 1.5 compliance on a Mac.
Seems like we are splitting hairs. The syntax shouldn't be that fragile.
So, if I stand on my head, and touch my right elbow, and hold down the Shift key . . . .
Campbell Ritchie wrote:Welcome to the Ranch
![]()
I wasn’t aware of that class; there are all sorts of useful things hiding away in the Apache project.
Consider Paul's rocket mass heater. |