strange behavior with Arrays.sort()
in
Programming Questions
•
1 year ago
Hello all,
When using the Arrays.sort() function on String array b, String array a is also sorted.
Could someone explain me why?
- String[] a = {
- "dg","a","swdtgf","dwghf","dfddfg","vrt","aze","ccx","gt","njk","kiuylloo","b"
- };
- println(a);
- /*
- prints:
- [0] "dg"
- [1] "a"
- [2] "swdtgf"
- [3] "dwghf"
- [4] "dfddfg"
- [5] "vrt"
- [6] "aze"
- [7] "ccx"
- [8] "gt"
- [9] "njk"
- [10] "kiuylloo"
- [11] "b"
- */
- String[] b=a;
- println(b);
- /*
- prints:
- [0] "dg"
- [1] "a"
- [2] "swdtgf"
- [3] "dwghf"
- [4] "dfddfg"
- [5] "vrt"
- [6] "aze"
- [7] "ccx"
- [8] "gt"
- [9] "njk"
- [10] "kiuylloo"
- [11] "b"
- */
- Arrays.sort(b);
- println(a);
- /*
- prints:
- [0] "a"
- [1] "aze"
- [2] "b"
- [3] "ccx"
- [4] "dfddfg"
- [5] "dg"
- [6] "dwghf"
- [7] "gt"
- [8] "kiuylloo"
- [9] "njk"
- [10] "swdtgf"
- [11] "vrt"
- */
- println(b);
- /*
- prints:
- [0] "a"
- [1] "aze"
- [2] "b"
- [3] "ccx"
- [4] "dfddfg"
- [5] "dg"
- [6] "dwghf"
- [7] "gt"
- [8] "kiuylloo"
- [9] "njk"
- [10] "swdtgf"
- [11] "vrt"
- */
http://makio.free.fr/
1