arraylist question
Posted: Mon May 13, 2019 8:49 pm
I was looking around for ArrayList manipulation examples and came across this code. I get all of it, except for the fact that why the last w2 assignment stays the same and not pickup the new ws1 String object. Some hint(s) please? Thanks
Code: Select all
ArrayList <String> mylArray = new ArrayList<>();
myArray.add("coke");
myArray.add("pepsi");
myArray.add("mountain dew");
System.out.println("Total ArrayList: "+myArray);
String[] ws1 = new String[myArray.size()];
String[] ws2 = myArray.toArray(ws1);
System.out.println("ws1 = ws2: "+(ws1 == ws2)); //true
System.out.println("ws1: "+Arrays.toString(ws1));
System.out.println("ws2: "+Arrays.toString(ws2));
ws1 = new String[1];
ws1[0] = "filler";
ws2 = myArray.toArray(ws1);
System.out.println("ws1 = ws2: "+(ws1 == ws2)); //false
System.out.println("ws1: "+Arrays.toString(ws1));
System.out.println("ws2: "+Arrays.toString(ws2));