printing arrays

2020-02-14 10:42发布

问题:

Okay, I'm trying to get file scanner to return the array itemList. However, I don't understand why everytime I get to return itemList and try with a println form. It returns:

[Ljava.lang.String;@3d3b5a3a

[Ljava.lang.String;@10cb42cf

[Ljava.lang.String;@482d59a3

[Ljava.lang.String;@18f42160

When the file I'm reading it contains stuff like

apples 10

fish 20

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

class loadInventory{  /*This class is used for loading the inventory */

private String[] itemList;  /*The lines of the inventory are stored in this array */
private int numItems;       /* The number of items is stored in this variable */

public loadInventory(String fileName){  /*We can do everything in the constructor. It gets the fileName from the superMarket
                                              object */

 itemList = new String[100];  /*We assume there are not more than 100 items in the inventory */
 numItems=0;                  /*initialize numItems to 0*/
                         /*Read the file using the try-catch block below. We are not specifically catching any exception.
                               We will not cover reading or writing of files and exceptions in this unit. So you 
                              don't need to understand this piece of code. */
 try{
     BufferedReader reader = new BufferedReader(new FileReader(fileName)); /*standard code for reading a file */
     String line = reader.readLine();        /*read the next line from the file and store in line */
     while (line != null){                  /*as long as there are lines */
          itemList[numItems]= line;         /*store the line in the current location of the array */
          numItems++;                       /*increment the number of items */
          line = reader.readLine();         /*read the next line */
       }

      reader.close();           /*close the reader */
   } catch (IOException e){     /*we don't catch any exception */
       }
       System.out.println(itemList);
  }

  public String[] getItemList() {
        return itemList;

    }


} 

回答1:

Print the array of instances like this:

System.out.println(Arrays.toString(itemList)); 


回答2:

An array itself uses the default toString() from Object, so it does not print its contents. You will need to use java.util.Arrays.toString(Object[]) to print out the content of the array (or loop over it yourself).



回答3:

int length = itemList.length;

int count =0;

while(count<length)
{
 System.out.println(itemList[count]);
 count++;
}

thats a simplest way to iterate the list.



回答4:

Because you are trying to print the whole array object while doing:

System.out.println(itemList);

Instead you need to print the single String elements stored inside the array:

System.out.println(Arrays.toString(itemList));


回答5:

Don't print the String[] value directly. use like this,

for (int i = 0; i < itemList.length; i++) {
System.out.println("Value::> " +itemList[i]);
}


回答6:

Pay attention on this code

while (line != null)
{                 
      itemList[numItems]= line;        
      numItems++;                      
      line = (String)reader.readLine();        
}

Try this.



标签: java arrays