打印ArrayList(我的数据类型)使用For Each Loop不工作

时间:2017-02-27 16:53:21

标签: java arraylist

这是我的UserDetail类代码。

public class UserDetail {

    public void user_detail() {

        Members m1=new Members("Rashid Faheem","0312-6193172","House No. 430, Street No. 5 Mehmood Abad Pindora, Rawalpindi");
        Members m2=new Members("Yawar Hayat","0312-6193172", "RajanPur");
        Members m3=new Members("Azhar Malik", "0312-6193172", "RajanPur");
        Members m4=new Members("Muhammad Ali", "0312-6193172", "RajanPur");
        Members m5=new Members("Muhammad Nazik", "0312-6193172", "RajanPur");

        ArrayList<Members> al = new ArrayList<Members>();

        al.add(m1);
        al.add(m2);
        al.add(m3);
        al.add(m4);
        al.add(m5);

        System.out.println("These are our Members.");

        For (Members m:al) {

            System.out.println(m.getName());
        }


    }


}

以下是来自Members Class的代码,我将其用作ArrayList中的DataType。

public class Members {

    private String name, phone,address;


    public Members(String name, String phone, String address) {
        name=name;
        phone=phone;
        address=address;

    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }
}

更新 我根据你的建议更改了我的代码但现在又出现了另一个问题我只是得到了名字,但没有电话和地址。

输出

These are our Members.
Rashid Faheem
Yawar Hayat
Azhar Malik
Muhammad Ali
Muhammad Nazik

4 个答案:

答案 0 :(得分:0)

for (Members m:al) {

    System.out.println(m.getName());
}

注意小写f。只是一个错字。

编辑:

您的构造函数也只是将构造函数参数赋给自己。您需要将构造函数参数分配给类变量。

public Members(String name, String phone, String address) {
    this.name = name;
    this.phone = phone;
    this.address = address;
}

答案 1 :(得分:0)

您应该替换For by for。 并向会员提供更多信息:

public Members(String name, String phone, String address) 
{
    this.name=name;
    this.phone=phone;
    this.address=address;
}

答案 2 :(得分:0)

有两种语法错误。一旦你纠正它们,你应该能够打印成员列表:

  1. for for For。

    for (Members m : al) {
        System.out.println(m.getName());
    }
    
  2. 成员构造函数。代码不会将参数分配给成员变量。

    public Members(String name, String phone, String address) {
        this.name=name;
        this.phone=phone;
        this.address=address;    
    } 
    
  3. 参考https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

答案 3 :(得分:0)

首先,您应该坚持Java naming conventions - 所以您的方法user_detail应该命名为userDetail。您可能需要考虑将其命名为print,因为它应该是它应该做的。

接下来,Members类中的构造函数不执行任何操作,因为您错过了this.作为操作左侧的前缀。所以你想要这样写(如 WatchdogReset 所述):

public Members(String name, String phone, String address) {
    this.name = name;
    this.phone = phone;
    this.address = address;
}

另一个问题是For中的拼写错误应该是for(由 ninnemank 表示)。

我重构了你的代码,向你展示了做你想做的事情的可能方式 - 当然这是相当自以为是的,而不是一个完美的解决方案。

<强>成员

public class Members {
    private String name;
    private String phone;
    private String address;

    public Members(String name, String phone, String address) {
        this.name = name;
        this.phone = phone;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }

    @Override
    public String toString(){
        return String.join(" / ", this.getName(), this.getPhone(), this.getAddress());
    }
}

<强> UserDetail

import java.util.ArrayList;
import java.util.List;

public class UserDetail {
    private List<Members> members;

    public UserDetail() {
        this.members = new ArrayList<>();
    }

    public boolean addMember(Members member){
        return this.members.add(member);
    }

    @Override
    public String toString(){
        return this.members.stream()
                .map(member -> member.toString())
                .reduce((a, b) -> String.join(System.lineSeparator(), a, b))
                .orElse("");
    }
}

主要

public class Main {
    public static void main(String[] args) {
        Members m1 = new Members("Rashid Faheem", "0312-6193172", "House No. 430, Street No. 5 Mehmood Abad Pindora, Rawalpindi");
        Members m2 = new Members("Yawar Hayat", "0312-6193172", "RajanPur");
        Members m3 = new Members("Azhar Malik", "0312-6193172", "RajanPur");
        Members m4 = new Members("Muhammad Ali", "0312-6193172", "RajanPur");
        Members m5 = new Members("Muhammad Nazik", "0312-6193172", "RajanPur");

        UserDetail userDetail = new UserDetail();

        userDetail.addMember(m1);
        userDetail.addMember(m2);
        userDetail.addMember(m3);
        userDetail.addMember(m4);
        userDetail.addMember(m5);

        System.out.println("These are our Members.");
        System.out.println(userDetail);
    }
}

<强>输出

These are our Members.
Rashid Faheem / 0312-6193172 / House No. 430, Street No. 5 Mehmood Abad Pindora, Rawalpindi
Yawar Hayat / 0312-6193172 / RajanPur
Azhar Malik / 0312-6193172 / RajanPur
Muhammad Ali / 0312-6193172 / RajanPur
Muhammad Nazik / 0312-6193172 / RajanPur