简体   繁体   English

ArrayList用于循环打印索引而不是值

[英]ArrayList for loop printing out indexes instead of value

I've created a method to read a text file and pull out the name of a contact from each line. 我创建了一种方法来读取文本文件,并从每一行中提取联系人的姓名。

private  ArrayList<String> readContacts()
    {
        File cFile = new File ("Contacts.txt");
        BufferedReader buffer = null;
        ArrayList <String> contact = new ArrayList<String>();
        try
        {
            buffer = new BufferedReader (new FileReader (cFile));
            String text;
            String sep;
            while ((sep = buffer.readLine()) != null)
            {       
                String [] name = sep.split (",");
                text = name[1];
                contact.add(text);  
            }   
        }
        catch (FileNotFoundException e)
        {

        }
        catch (IOException k)
        {

        }
        return contact;
    }

I'm trying to populate a JList with each contacts name using the method I've created above using this: 我正在尝试使用上面创建的方法使用每个联系人姓名填充一个JList

model = new DefaultListModel();
for (int i = 1; i < readContacts().size(); i++)
{
  ArrayList <String> name = readContacts();
  model.addElement(name);
}

nameList = new JList (model);
add(nameList);

When I run the program, the JList only has the numbers 1-10, instead of each of the contacts names. 当我运行程序时, JList仅具有数字1-10,而不是每个联系人名称。 Is the problem I'm running into here logical or problems with syntax? 我遇到的问题是逻辑问题还是语法问题? Any help would be great, thanks! 任何帮助将是巨大的,谢谢!

  1. Don't call readContacts() from within the for loop as that makes no sense. 不要叫readContacts()为没有意义的循环,从 You're creating a new ArrayList multiple times and then adding the entire same ArrayList to your JList, in other words, each element in your JList is an ArrayList (???). 您要创建一个新的ArrayList多次,然后将整个相同的ArrayList添加到JList中,换句话说,JList中的每个元素都是一个ArrayList(???)。
  2. Instead call it in the for loop condition or before the for loop. 而是在for循环条件中或for循环之前调用它。
  3. Do not have empty catch(...) blocks. 没有空的catch(...)块。 Doing this is the programming equivalent of driving your car with your eyes closed -- very dangerous. 这样做相当于在闭着眼睛开车时的编程效果-非常危险。

For example, 例如,

model = new DefaultListModel();
// call readContacts() only *once*
for (String name: readContacts()) {
    model.addElement(name);
}

I would say we may create a ArrayList object first, then assign the new readContacts() to this object. 我会说我们可以先创建一个ArrayList对象,然后将新的readContacts()分配给该对象。 After that, read the elements in a loop, if you want, you can print out each elements in the ArrayList to ensure those are you want. 之后,如果需要,循环读取元素,可以打印出ArrayList中的每个元素,以确保它们是您想要的。

If your program is running, you do not have a syntax problem. 如果您的程序正在运行,则没有syntax问题。 Your program is running, but is not giving the expected results. 您的程序正在运行,但是没有给出预期的结果。 That is a logical problem. 这是一个logical问题。

You state: 您声明:

"the JList only has the numbers 1-10, instead of each of the contacts names." “ JList仅具有数字1-10,而不是每个联系人名称。”

When I look at your readContacts() , you populate your contact List with name[1]. 当我查看您的readContacts() ,您会在联系人List填充名称[1]。 What is in name[1]? 名称是什么[1]? I'm lead to believe that name[1] contains 1 - 10 as you read through your file. 我相信您在读文件时name [1]包含1-10。 What is in name[0], or name[2] or name[3], if they even exist. 名称[0]或名称[2]或名称[3](如果它们甚至存在)中的内容。 So my first suggestion will be to try a different index for name since name[1] isn't giving you the right result. 因此,我的第一个建议是尝试使用不同的名称索引,因为name [1]不能给您正确的结果。

Second, you populated your model with the ArrayList name, instead of using the contents of name. 其次,使用ArrayList名称填充model ,而不使用name的内容。 Follow Hovercraft Full Of Eels example to populate your model. 按照“气垫船充满鳗鱼”示例填充模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM