Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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 使用toString和增强的for循环更好地显示ArrayList中的信息_Java_For Loop_Arraylist - Fatal编程技术网

Java 使用toString和增强的for循环更好地显示ArrayList中的信息

Java 使用toString和增强的for循环更好地显示ArrayList中的信息,java,for-loop,arraylist,Java,For Loop,Arraylist,我希望能够使用联系人类的联系人列表显示联系人列表中的信息 我有一个ContactArrayList类,它保存一个Contact类对象。在ContactArrayList类中,我有一个add,remove,size,isEmpty等,。将与其他方法一起用于ContactArrayList类中的ContactArrayList的类的方法 在我的主/驱动程序类中,我为ContactArrayList类创建了一个对象,并创建了Contact类的一个“用户”对象和几个“固定”对象 我的问题是: 当用户选择

我希望能够使用
联系人
类的
联系人列表
显示联系人列表中的信息

我有一个
ContactArrayList
类,它保存一个
Contact
类对象。在
ContactArrayList
类中,我有一个
add
remove
size
isEmpty
等,。将与其他方法一起用于
ContactArrayList
类中的
ContactArrayList
的类的方法

在我的主/驱动程序类中,我为
ContactArrayList
类创建了一个对象,并创建了
Contact
类的一个“用户”对象和几个“固定”对象

我的问题是: 当用户选择显示所有联系人的信息时,包括罐装对象和用户对象,我尝试使用
ContactArrayList
类的增强for循环W/a
toString
方法,但因为我使用的是增强for循环,使用的是
Contact
类“迭代器”变量来浏览和显示信息。当我想使用
ContactArrayList
toString
时,它正在使用
Contact
toString

ContactArrayList
联系方式
: 输出:
总的来说,我会继续解决这个问题,它可能很简单,但希望有人指出显而易见的问题。如果您需要更多关于ContactArrayList类、Contact类或Main/driver类的信息,请告诉我

您已经在ArrayList的
toString
方法中循环。所以你不该这么做吗

cal1.toString();
而不是

for(Contact display : cal1.contactArray)
{
    System.out.println(display.toString());
}

您已经在ArrayList的
toString
方法中循环。所以你不该这么做吗

cal1.toString();
而不是

for(Contact display : cal1.contactArray)
{
    System.out.println(display.toString());
}
toString()
方法是java用来生成
字符串,供开发人员调试。我建议您要么实现自己的
toReadableString()
,要么简单地定义如何在现场渲染它。Java 8提供了一些很好的功能:

case 2:
    String s = contacts.stream()
            .map(c -> Stream.of(c.getName(), c.getLastName(), c.getPhoneNumber(), c.getEmailAddress())
                    .collect(Collectors.joining(", ")))
            .collect(Collectors.joining("\n\t", "Displaying all contacts and information:\n\t", ""));
    System.out.println(s);
    break; 
首先,我们从
联系人
创建一个
。然后,我们使用
map
联系人的
转换为
字符串的
。我们再次创建四个值的
,并将它们与
连接起来。第二个
流将创建每个联系人

然后我们回到外部
,在那里我们现在有了一个可读联系人的
流。我们也加入它们,用
“\n\t”
分隔它们,从而创建一个
字符串,如下所示:

Displaying all contacts and information:
    Mike, Dim, 123456789, email
    Foo, Bar, 987654321, hello@wor.ld
@Override
public int compareTo(Contact other) {
    if (this.lastName.compareTo(other.lastName) == 0) {
        return this.name.compareTo(other.name);
    } else {
        return this.lastName.compareTo(other.lastName);
    }
}
toString()
方法是java用来生成
字符串,供开发人员调试。我建议您要么实现自己的
toReadableString()
,要么简单地定义如何在现场渲染它。Java 8提供了一些很好的功能:

case 2:
    String s = contacts.stream()
            .map(c -> Stream.of(c.getName(), c.getLastName(), c.getPhoneNumber(), c.getEmailAddress())
                    .collect(Collectors.joining(", ")))
            .collect(Collectors.joining("\n\t", "Displaying all contacts and information:\n\t", ""));
    System.out.println(s);
    break; 
首先,我们从
联系人
创建一个
。然后,我们使用
map
联系人的
转换为
字符串的
。我们再次创建四个值的
,并将它们与
连接起来。第二个
流将创建每个联系人

然后我们回到外部
,在那里我们现在有了一个可读联系人的
流。我们也加入它们,用
“\n\t”
分隔它们,从而创建一个
字符串,如下所示:

Displaying all contacts and information:
    Mike, Dim, 123456789, email
    Foo, Bar, 987654321, hello@wor.ld
@Override
public int compareTo(Contact other) {
    if (this.lastName.compareTo(other.lastName) == 0) {
        return this.name.compareTo(other.name);
    } else {
        return this.lastName.compareTo(other.lastName);
    }
}

感谢您提供缺失的课程。 问题在于您的
联系人
班级:

private static String name = " ";
private static String lastName = " ";
private static String phoneNumber = " ";
private static String emailAddress = " ";
这些变量都是静态的,这意味着它们不是每个联系人都存在一次,而是每个应用程序都存在一次。因此,所有
联系人
都将共享相同的
姓名
姓氏
,等等

如果删除
静态
修改器,它应该可以工作

但您的代码中还有一些我想解决的问题:

  • 不要那样叫你的
    ContactArrayList
    。其他开发人员将研究它,并期望它扩展
    ArrayList
    ,但它没有扩展。只需将其称为
    联系人
    ,这就更好了(我会在这里将其称为表单)
  • 您不应该使用
    toString
    来显示用户可读的文本。它用于输出文本以进行调试。将
    toString
    方法替换为以下方法:

    Displaying all contacts and information:
        Mike, Dim, 123456789, email
        Foo, Bar, 987654321, hello@wor.ld
    
    @Override
    public int compareTo(Contact other) {
        if (this.lastName.compareTo(other.lastName) == 0) {
            return this.name.compareTo(other.name);
        } else {
            return this.lastName.compareTo(other.lastName);
        }
    }
    
  • 联系方式

    public String toReadableString() {
        return "Name: " + this.name + " " + this.lastName + ", phone number: " + phoneNumber + ", email: " + this.emailAddress;
    }
    
  • 不要调用您的
    ArrayList
    contactArray
    。它不是数组。称之为
    成员

  • Contacts
    ->您的
    toString
    方法已损坏。您刚刚将每个
    联系人
    的结果存储在同一
    ToString
    中(这也是一个坏名字。我不知道这是什么意思)

  • 您的
    addContact(String passedString)
    方法已断开。我不知道它应该做什么,但它只创建了一个新的
    ArrayList
    ,您永远不会用它做任何事情
  • 请将
    .indexOf(passedString)>-1
    替换为
    .contains(passedString)
    。它可能做同样的事情,但其中一个更容易阅读
  • 我不太确定
    Contact
    中的方法
    publicstaticstringuserinput()
    应该做什么。看起来你可以摆脱它
  • 您对
    联系人的继承是错误的。它应该是
    Contact-extends-compariable

  • 您的
    compareTo
    方法不正确。将其替换为以下内容:

    Displaying all contacts and information:
        Mike, Dim, 123456789, email
        Foo, Bar, 987654321, hello@wor.ld
    
    @Override
    public int compareTo(Contact other) {
        if (this.lastName.compareTo(other.lastName) == 0) {
            return this.name.compareTo(other.name);
        } else {
            return this.lastName.compareTo(other.lastName);
        }
    }
    
  • sort
    方法替换为
    Collections.sort(members)(您可以这样做,因为
    联系人
    是p