java - Collection.sort(arraylist) in a function sorting another arraylist -


Say, I have created an array list in (public square class 1 [string array [])

< double> <1> new1 = new arreelist & lt; double & gt; ();

Then I will assign this array to a function (public constant Passing zero main (string error [])

  biggest value (list1);  

This is the function for example:

  Public Stable Double The Largest Value (List & quot; Double & gt; Su Chi) {Collections.sort (list); return list.get (list.size () - 1);}  

I pass it in a function so that it is expected Will list only the list but not list 1 but list 1 is also sorted, and I do not understand why this is why, please explain to me why this is, and what is the solution to this error?

In the context of only list when you pass it as argument In areas so both list indicates and list1 same List .

A good rule of thumb is not to modify the object passed in a method, so I will make a copy inside the method:

  public stable double largest value (list & Lt; double & gt; list) {List & lt; Double & gt; Temp = new arreelist & lt; & Gt; (List); Collections.sort (Temporary); Return temp.get (temp.size () - 1); }  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -