-9

The toCharArray() in Java returns the Character Array and Arrays.toString() converts a array to string and toString() does same but why this below code doesn't work.

public static void main(String[] args) {

    ArrayList<String> ar = new ArrayList<>(Arrays.asList( "abc", "def", "ghi", "jkl"));

    char[] t = "abc".toCharArray();
    String t2 = Arrays.toString(t);

    if( ar.contains(t2)){
        System.out.println("contains"); // This line doesn't get executed
    }

    String t3 = t.toString();
    if( ar.contains(t3)){
        System.out.println("contains"); // This line doesn't get executed
    }

    if( ar.contains("abc")){
        System.out.println("contains"); // This line execute and prints on console
    }
}
Aarrav Agarwal
  • 81
  • 1
  • 1
  • 9
  • So have you looked at the actual value or `ar`? I think that will make things obvious. – Jon Skeet Oct 11 '20 at 14:54
  • The code works, and does exactly what it should. `System.out.println(t2);` will tell you why it doesn't do what you think it should do. – JustAnotherDeveloper Oct 11 '20 at 14:55
  • "and toString() does same" not exactly. It is true that it also *returns* a String, but did you check what is the *content* of that String? If you want to create String from char[] you can use `new String(charArray)`. – Pshemo Oct 11 '20 at 14:57
  • Related: [How to convert a char array back to a string?](https://stackoverflow.com/q/7655127), [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/q/29140402) – Pshemo Oct 11 '20 at 15:00
  • Also please try to avoid term "doesn't work". More info: https://web.archive.org/web/20180124130721/http://importblogkit.com/2015/07/does-not-work/ – Pshemo Oct 11 '20 at 15:05
  • When I printed out t2 it results out _[C@4c203ea1_, whereas when I print Arrays.toString(t), it prints _['a', 'b', 'c']_, so what toString is used for? – Aarrav Agarwal Oct 11 '20 at 15:20
  • Possibly related: [How to use the toString method in Java?](https://stackoverflow.com/q/3615721). In short we use `toString()` to let methods like `println(someObject)` some *recipe* to generate string representation of any object passed as argument. So internally it will call `someObject.toString()` and will print that to console/file/wherever it was set to. So each time we design a class we can *override* `toString` method of it like `class Point{private int x, y; ... public toString(){ return "Point: x="+x+", y="+y;} ...}`. – Pshemo Oct 11 '20 at 15:40
  • But if we don't override that method its implementation will be inherited from closest ancestor which provides such implementation, which can also be an Object class where `toString` is implemented to `return getClass().getName() + '@' + Integer.toHexString(hashCode());`. Since arrays don't override it that is what you are seeing. So at `[C@4c203ea1` `[C` represents *one dimensional* `[` array of `char` (`C`) and after @ you see hashcode of that array. – Pshemo Oct 11 '20 at 15:40

4 Answers4

1

toString() called on a char array doesn't do the same.

When you write

String t3 = t.toString();

you said that you want to call toString() method from Object.class. Because char array cannot have an overrided version of toString() that will do what you re expected, because it's an array of primitive type. It works as expected, it returns you the string representation of a link to that particular char array in a heap.

Arrays.toString(t) returns you the string representation of an initial array. In your case it will return "[a, b, c]". Then when you're trying to call ar.contains(t2) you re trying to find "[a, b, c]" string in your initial array which doesn't have it. It have 4 strings inside. "abc", "def", "ghi", "jkl". It doesn't contain the requested element ("[a, b, c]" string).

I would recommend you to debug your program. You will see which values are actually in your variables. It will give you a lot more understanding about this situation.

maveriq
  • 456
  • 4
  • 14
0

Arrays.toString returns [a,b,c] which is not equal to abc. See above answers why the other one didn't work.

koleon03
  • 71
  • 8
  • I think, you mean to say."toString()" and "Arrays.toString()" does the same. – Aarrav Agarwal Oct 11 '20 at 15:06
  • @AarravAgarwal "*"toString()" and "Arrays.toString()" does the same*" why? It is clearly not true and even example in your question disproves it. Did you actually check results of those methods? – Pshemo Oct 11 '20 at 15:09
0

You are assuming that t.toString() will return a String that is made up of all characters in the array t. This is not the case, and nor does the language require it to be.

What you want is String t3 = new String(t)

user14387228
  • 391
  • 2
  • 4
0

The code works exactly as expected. The ideal way to do this would be

String t2 = String.valueOf(t);
if(ar.contains(t2)){
    System.out.println("contains"); // This line execute and prints on console
}
unc0ded
  • 111
  • 7