Java list output question.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

I want to put the array in the list and print out the list.

If you run the code below, " The error [Ljava.lang.String;@76ed5528] is displayed.

When printing, the same result comes out even if you attach toString(), but the cause is not found even if you google it. Please give us feedback.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


class Main {
  public static void main(String[] args) {

//    1. Convert the array to List
//  2. Using the add() method in the list,
// Add a new value to the desired location
// 3. The list has been converted back to an array.

// Create Array
  String[] abc = {};
  String[] abc2 = {};

//    1. Convert the array to List
  List<String> list = new ArrayList<>(Arrays.asList(abc));

//  2. Use the add() method in the list to add a new value to the desired location
  list.add(0, "1230000");
  list.add(1, "ajshfk");
  list.add(2, "skhkgkg");
  list.add(3, "skhkgkg1");
  list.add(4, "skhkgkg2");
  list.add(5, "skhkgkg3");
  list.add(6, "skhkgkg5");

  list.add(0, "12300001");
  list.add(1, "ajshfk1");
  list.add(2, "skhkgkg1");
  list.add(3, "skhkgkg11");
  list.add(4, "skhkgkg21");
  list.add(5, "skhkgkg31");
  list.add(6, "skhkgkg51");

// 3. The list has been converted back to an array.
  abc = list.toArray(abc);
  abc2 = list.toArray(abc2);

// Put an array in the list
List<String[]> listLst = new ArrayList<String[]>();
  Collections.addAll(listLst, abc);
  Collections.addAll(listLst, abc2);
 //  //  System.out.println(list1.**toString()**); 

// To print out a list 
if(list2 != null){
  for(int i = 0; i < list2.size(); i++){
      System.out.print(list2.get(i));  // System.out.print(list2.get(i).**toString()**); 
  }
}

  }
}

java

2022-09-20 11:17

1 Answers

I don't know what list2 is, so I'm answering with a guess.

[Ljava.lang.String;@76ed5528 is not an error. All classes in Java inherit the Object, and if toString() is not defined separately in the class you are trying to output, call Object.toString().

Object.toString() looks like this:

public String toString() {
      return getClass().getName()+"@"+Integer.toHexString(hashCode());
}

It's supposed to print out the class name and hash value, right?

If it had been properly written to output the variable containing the String class, the string would have been printed, not [Ljava.lang.String;@76ed5528, but I think it was trying to print the list as it is.

Use Arrays.toString() when you want to print the list as it is.

https://www.geeksforgeeks.org/arrays-tostring-in-java-with-examples/


2022-09-20 11:17

If you have any answers or tips


© 2022 pinfo. All rights reserved.