印刷阵列 [英] printing arrays

查看:114
本文介绍了印刷阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我试图让文件扫描返回数组ITEMLIST。
不过,我不明白为什么每次我去回报ITEMLIST和一个println形式尝试。
它返回:

  [Ljava.lang.String; @ 3d3b5a3a[Ljava.lang.String; @ 10cb42cf[Ljava.lang.String; @ 482d59a3[Ljava.lang.String; @ 18f42160

当我读它的文件中包含的东西,如

苹果10

20鱼

 进口java.io. *;
进口的java.util。*;类loadInventory {/ *这个类是用于装载清单* /私有String [] ITEMLIST; / *库存的行存储在该数组中* /
私人诠释numItems的; / *的项数存储在该变量* /公共loadInventory(字符串文件名){/ *我们可以在构造函数中的一切。它得到的文件名从超市
                                              对象* / ITEMLIST =新的String [100]; / *我们假设有没有存货* 100余项/
 numItems的= 0; / * numItems的初始化为0 * /
                         / *读取使用下面的try-catch块文件。我们不是专门捕获任何异常。
                               我们将不包括在本单位的文件和例外的读取或写入。那么你
                              并不需要了解这片code的。 * /
 尝试{
     读者的BufferedReader =新的BufferedReader(新的FileReader(文件名)); / *标准code读取文件* /
     串行= reader.readLine(); / *读取文件的下一行,并线*存储/
     而(行!= NULL){/ *只要有行* /
          ITEMLIST [numItems的=行; / *存储线在所述阵列的当前位置* /
          numItems的++; / *递增的项目数* /
          行= reader.readLine(); / *读取下一行* /
       }      reader.close(); / *关闭读者* /
   }赶上(IOException异常五){/ *我们不赶任何异常* /
       }
       的System.out.println(ITEMLIST);
  }  公众的String [] getItemList(){
        返回ITEMLIST;    }
}


解决方案

打印实例像这样的数组:

 的System.out.println(Arrays.toString(ITEMLIST));

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;

    }


} 

解决方案

Print the array of instances like this:

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

这篇关于印刷阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆