0

Int a[] = {1,2};

String s = a.toString(); //what's going on here.

My output is

I@something for integer array. C@something for char array.

How to I correctly convert any array into string.

Dev Shukla
  • 13
  • 2
  • How do you want to convert? Do you want to merge and convertor do you want to convert it individually? – Alpit Anand Apr 24 '20 at 08:31
  • Does this answer your question? [How to convert a char to a String?](https://stackoverflow.com/questions/8172420/how-to-convert-a-char-to-a-string) – Alpit Anand Apr 24 '20 at 08:32
  • Please read **[Convert Character Array To String In Java](https://www.tutorialcup.com/java/convert-char-array-to-string-in-java.htm)** this will help you answer your question – Rahul Gupta May 14 '21 at 17:31

3 Answers3

1

You can use Arrays.toString(array)

Like for example :

Int a[] = {1,2};

System.out.println("Array as String:"+Arrays.toString(array))

It will output:

Array as String:[1,2]

Dheeraj Joshi
  • 1,522
  • 14
  • 23
  • What does string s store in itself.? When I print s it only gives me a string which is = I@4517d9a3 if array is integer. If array is of character then my output for string s is C@4517d9a3, similarly for double array, I get string a output as D@4517d9a3 – Dev Shukla Apr 24 '20 at 10:29
  • @DevShukla it will give you the address of the array – Dheeraj Joshi Apr 24 '20 at 11:57
0

Have you tried using char[] array? If it suits your need this would help you:

char[] chars = {'1', 'a'};

    String string = new String(chars);
    System.out.println(string);

Output: 1a

developer1
  • 527
  • 4
  • 27
  • Thanks!, well this helped me. But now only query that left is when i try String s = a.toString(); What does string s store in itself.? When I print value of s it only gives me a string which is = I@4517d9a3 if array is integer. If array is of character then my output for string s is C@4517d9a3, similarly for double array, I get string a output as D@4517d9a3 what are these codes. – Dev Shukla Apr 24 '20 at 10:54
0

You can create your own implementation for learning what is going on background.

int a[] = {1,2};
printArray(a);

public void printArray(int[] a) {
    System.out.print("Array={");
    for(int singleElement : a) {
        print(singleElement);
    }
    System.out.println("}");
}
Murat Karagozgil
  • 178
  • 1
  • 2
  • 13