Getting error on printing array

后端 未结 4 570
孤独总比滥情好
孤独总比滥情好 2021-01-28 12:22

I got this code:

import java.util.*;
import java.io.*;

public class Oblig3A{
    public static void main(String[]args){
    OrdAnalyse O = new OrdAnalyse();
            


        
4条回答
  •  旧时难觅i
    2021-01-28 13:01

    This is not an error... This is what the default toString() implementation of the Object class returns...

    [Ljava.lang.String;@163de20
    

    Means:

    • array of references ( [L )
    • of type String ( java.lang.String )
    • unique object ID

    Code of Object.toString()

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

    What you shouldd do is to use a proper way to print:

    • a loop

      StringBuilder sb = new StringBuilder();
      for(String s: myArray) {
          sb.append(s);
          if(sb.length()>0) {
             sb.append(',');
          }
      }
      System.println(s.toString());
      
    • Arrays.toString

提交回复
热议问题