Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:数组只打印最后一个元素_Java_Arrays - Fatal编程技术网

Java:数组只打印最后一个元素

Java:数组只打印最后一个元素,java,arrays,Java,Arrays,我被这项任务耽搁了好几个小时,我搞不懂。我创建了一个艺术家数组,其大小由一个变量定义,随着更多艺术家的加入,该变量会增加。如果我设置artist[]artistList=new artist[totalArtist],我得到一个边界数组或只是一个空白输出,这样做artist[]artistList=new artist[1+totalArtist]对我有效,至少给了我一个输出。任何改进都很好 以下是我的代码片段: public static void main(String[] args) {

我被这项任务耽搁了好几个小时,我搞不懂。我创建了一个艺术家数组,其大小由一个变量定义,随着更多艺术家的加入,该变量会增加。如果我设置
artist[]artistList=new artist[totalArtist],我得到一个边界数组或只是一个空白输出,这样做
artist[]artistList=new artist[1+totalArtist]对我有效,至少给了我一个输出。任何改进都很好

以下是我的代码片段:

public static void main(String[] args) {
        //initialize total artists and id
        int totalArtist = 0;
        int newID = 0;

        //create an array for artists
        artist[] artistList = new artist[1+totalArtist];

        //open the original file
        try {
            File file = new File("p1artists.txt");
            Scanner input = new Scanner(file);

            while (input.hasNextLine()) {
                int id = input.nextInt();
                String name = input.next();

                //create a new artist and put it in the array
                for (int i = 0; i < artistList.length; i++) {
                    artistList[i] = new artist(id, name);
                    totalArtist++;
                }
                //increment to a newID
                newID = id + 1;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

       for(artist e : artistList)
           System.out.println(e);
publicstaticvoidmain(字符串[]args){
//初始化艺术家总数和id
整数=0;
int newID=0;
//为艺术家创建阵列
艺人[]艺人列表=新艺人[1+总体艺人];
//打开原始文件
试一试{
File File=新文件(“p1artists.txt”);
扫描仪输入=新扫描仪(文件);
while(input.hasNextLine()){
int id=input.nextInt();
String name=input.next();
//创建一个新的艺术家并将其放入阵列中
for(int i=0;i
我的主要问题是:在for循环中,我能够创建一个新的艺术家,并将其放入artistList数组中。我还能够打印每个元素。但是,在try catch之外,它只打印最后一个元素一次。我不明白发生这种情况时我做错了什么


请不要建议使用数组列表,因为如果我能够为此任务执行此操作,我显然会执行此操作。

您的数组仅打印一个元素,因为您尝试写入位置2+,该位置退出try块,并跳到foreach循环,从而出现异常

数组中只有一个元素

int totalArtist = 0;
artist[] artistList = new artist[1+totalArtist]; // = new artist[1];
由于不能使用列表,因此可以

  • 读取文件的行数,然后创建数组
  • 提示从文件中读取的艺术家数量
  • 通过自己创建一个具有更大大小数组的新数组并复制元素来模拟列表

  • 试着这样做:

    public static void main(String[] args)
    {
        // create an array for artists
        artist[] artistList = new artist[0];
    
        // open the original file
        try {
            final File file = new File("p1artists.txt");
            final Scanner input = new Scanner(file);
    
            while (input.hasNextLine())
            {
                final int id = input.nextInt();
                final String name = input.next();
                // Skip to next line...
                input.netxLine();
    
                // Create a ne array, with one more element
                final artist[] newList = new artist[artistList.length + 1];
                // Copy the element references to the new array
                System.arraycopy(artistList, 0, newList, 0, artistList.length);
                // create a new artist and put it in the array
                newList[artistList.length] = new artist(id, name);
                // Replace old array with new one
                artistList = newList;
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    
       for(artist e : artistList)
            System.out.println(e);
    }
    

    你的艺人列表长度总是1。因此,你总是在更新第一个元素和唯一元素。数组不能动态扩展。要实现你想要的,请考虑ARLayLIST或LIKEDLIST,取决于你如何预测你所得到的列表中的项目的使用方式。

    欢迎堆栈溢出!看起来你需要向我们学习。e调试器。请自己解决一些问题。如果以后仍有问题,请随时回答更具体的问题。“能够打印每个元素”--代码在哪里执行此操作?数组不能更改大小。若要使用动态大小的“数组”,改为使用
    List
    s。实际上,您应该1)使用ArrayList 2)在调整文件大小之前先计算文件的行数array@cricket_007
    artisList
    只有一个元素,并且从未改变大小,所以最后两行只会打印一个值,这是最后一个设置值……好吧,这真的很低效:)高效将是
    列表
    实现,顺便说一句,ArrayList也一样,但不是一个接一个,而是有更大的步骤…我知道ArrayList会分期偿还。只是指出处理大文件不会很好地扩展,没有人说会…但对于这个任务,大约85行,它似乎适合…@UsagiMiyamoto感谢这一点!从未想过使用阵列拷贝。