Reputation:
That is the code:
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[]args) {
changeList();
System.out.println(Arrays.toString(example().toArray()));
}
public static void changeList() {
example().set(2, "Hi!");
}
public static List<String> example () {
String str = new String("word1|word2|word3|word4|word5");
return Arrays.asList(str.split("\\|"));
}
}
I want the ArrayList to be like this: [word1, word2, Hi!, word4, word5]
But it doesn't change... It prints[word1, word2, word3, word4, word5]
Is it even possible to change it that way? Thanks in advance!
Upvotes: 0
Views: 90
Reputation: 83527
Let's step through your code one line at a time to understand what it is doing:
public static void main(String[]args) {
changeList();
This calls the changeList()
method:
public static void changeList() {
example().set(2, "Hi!");
First call example()
:
public static List<String> example () {
String str = new String("word1|word2|word3|word4|word5");
return Arrays.asList(str.split("\\|"));
}
This creates a new List
and returns it back to changeList()
:
example().set(2, "Hi!");
Now we modify the returned List
by setting the element at index 2 to "Hi!"
. However, the modified list is immediately discarded because you have no reference to it any more.
Then we return back to main:
System.out.println(Arrays.toString(example().toArray()));
Now we call example again:
public static List<String> example () {
String str = new String("word1|word2|word3|word4|word5");
return Arrays.asList(str.split("\\|"));
}
This creates a new List
. This new list is in no way related to the list that was created when we called example()
the previous time.
If you want to keep the same list, you need to assign it to a reference in changeList()
and then return it:
public static void changeList() {
List<String> theList = example();
theList.set(2, "Hi!");
return theList;
}
Now main() needs to save a reference to the list returned by changeList()
:
public static void main(String[]args) {
List<String> mainList = changeList();
System.out.println(mainList);
}
(Note: You can print the List
directly. Your original version is much more complicated than necessary.)
Upvotes: 0
Reputation: 3018
The problem is that example
gives a brand new List
each time it's called. When you create the list in changeList
, it's a different List
than the one you create in main
, and changes made to one List
won't affect another.
One way to fix this is to:
changeList
method do what it says: take in a List
(rather than producing one from scratch) and change that List
.main
method pass in the List
to changeList
.public static void main(String[]args) {
List<String> list = example();
changeList(list);
System.out.println(Arrays.toString(list.toArray()));
}
public static void changeList(List<String> list) {
list.set(2, "Hi!");
}
Upvotes: 1
Reputation: 1725
public static void main(String[]args) {
System.out.println(Arrays.toString(changeList().toArray()));
}
public static List<String> changeList() {
List<String> list = example();
list.set(2, "Hi!");
return list;
}
public static List<String> example () {
String str = new String("word1|word2|word3|word4|word5");
return Arrays.asList(str.split("\\|"));
}
Upvotes: 0
Reputation: 48268
But it doesn't change... Is it even possible to change it that way?
yes, you change the list but you lost the ref of the returned collection since the method public E set(int index, E element)
returns E and not the list itself.. see the doc
as I said, move the return of example() into a variable that you can manipulate...
public static void main (String[] args) {
List<String> retVal = example();
System.out.println(retVal);
retVal.set(2, "Hi!");
System.out.println(retVal);
}
public static List<String> example () {
String str = new String("word1|word2|word3|word4|word5");
return Arrays.asList(str.split("\\|"));
}
Upvotes: 0