cash1981
08-20-2007, 06:52 PM
Hi I am wondering about something.
ArrayList<String yourList = new ArrayList<String>();
You have the static method Collections.sort(yourList); which sorts your list.
The call to Collections.sort() sorts your list and doesnt return a new object.
I am wondering how this is possible? In my book it says: Remember that the sort() methods for both the Collections class and the Arrays classes are static methods, and that they alter the objects they are sorting, instead of returning a different sorted object.
This statement is fine.
But I wanted to try something.
I created my own class
(pseudo code)
class A {
int test = 1;
main() {
B.increment(test);
System.out.println(test); //Still prints 1 (Doesnt get incremented)
}
class B {
static increment(int x) {
x++;
}
So my question is, how come the list I send in Collections.sort() gets sorted but my poor integer value doesnt get incremented? It seems I still have to return back the object. Can someone explain this please?
ArrayList<String yourList = new ArrayList<String>();
You have the static method Collections.sort(yourList); which sorts your list.
The call to Collections.sort() sorts your list and doesnt return a new object.
I am wondering how this is possible? In my book it says: Remember that the sort() methods for both the Collections class and the Arrays classes are static methods, and that they alter the objects they are sorting, instead of returning a different sorted object.
This statement is fine.
But I wanted to try something.
I created my own class
(pseudo code)
class A {
int test = 1;
main() {
B.increment(test);
System.out.println(test); //Still prints 1 (Doesnt get incremented)
}
class B {
static increment(int x) {
x++;
}
So my question is, how come the list I send in Collections.sort() gets sorted but my poor integer value doesnt get incremented? It seems I still have to return back the object. Can someone explain this please?