java - 打印阵列

标签 java arrays

好的,我正在尝试让文件扫描器返回数组 itemList。 但是,我不明白为什么每次我都会返回 itemList 并尝试使用 println 表单。 它返回:

[Ljava.lang.String;@3d3b5a3a

[Ljava.lang.String;@10cb42cf

[Ljava.lang.String;@482d59a3

[Ljava.lang.String;@18f42160

当我正在读取的文件包含类似内容

苹果10

鱼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;

    }


} 

最佳答案

打印实例数组,如下所示:

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

关于java - 打印阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964598/

相关文章:

java - 如何从计算机或从 URL 加载文件

Java布局问题

java - Java中二维数组的分割和转置

javascript - js中如何检查一个数组是另一个数组的子集?

php - 分解数据库字符串并将值与php中的另一个字符串进行比较

javascript - 访问节点 firebase 并检查值

java - Sudo 策略模式的缺点

java - 为什么不推荐使用 "new Date(int year, int month, int day)"?

javascript - 在带有注释的 Servlet 中找不到 CSS 和 JS

C 什么 % 符号访问数组?