简体   繁体   中英

How do I stop the array from showing all the null values in my loop?

so I'm a beginner at this programming stuff and I'm making a basic Contact Manager for practice. I have the add contact method perfect and I was just trying to figure out how to make it so that if there is no value in my array after a certain point. It won't print that line. Basically trying to stop the loop after the last known object in the array. My code i have for this is.

public static void viewAllContacts(){



    for (x = 0; x < 100; x++){


        System.out.println("Full name: " +name[x]);
        System.out.println("Number: " +number[x]);
        System.out.println("E-mail Address: " +email[x]);
        System.out.println("Home Address: " +address[x]);
        System.out.println("Birthday: " +birthday[x]);
        System.out.println("Nickname: " +nickname[x]);
        System.out.println(" "); //space so that way the contact list is a bit prettier 

    }
}

Assuming the the values in each of the array is synchronized (same number of elements) then simply

for (x = 0; x < 100; x++){

    if (name[x]  == null)
       break;

of course you could also use a field to hold the max number of elements and then only loop until this number.

for (x = 0; x < maxNumberOfElements; x++){

You should do two things:

The first is to create a class to keep all your information instead of keeping it in separate arrays - keeping track of one array per attribute is really cumbersome.

Person.java:

public class Person {
    // Attributes
    private String name;
    private String number;
    private String email;
    private String address;
    private String birthday;
    private String nickname;

    // Constructor
    public Person(String name, String number, String email, String address,
            String birthday, String nickname) {
        this.name = name;
        this.number = number;
        this.email = email;
        this.address = address;
        this.birthday = birthday;
        this.nickname = nickname;
    }

    // Getters and setters

    public String getName() {
          return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }

    // And so on for all attributes
}

Then you should store your Person objects in an ArrayList so the size of the list matches how many Person objects you have. You can then use a foreach loop to iterate over the list:

ArrayList<Person> persons = new ArrayList<>();

persons.add(new Person("John Doe", "555-12345", "john@doe.com", "Fake Street 999",
        "1970-01-01", "Johnny"));
persons.add(new Person("Jane Doe", "555-12345", "jane@doe.com", "Fake Street 999",
        "1999-12-12", "Janey"));

for(Person person : persons) {
    System.out.println("Full name: " + person.getName());
    System.out.println("Number: " + person.getNumber());
    System.out.println("E-mail Address: " + person.getEmail());
    System.out.println("Home Address: " + person.getAddress());
    System.out.println("Birthday: " + person.getBirthday());
    System.out.println("Nickname: " + person.getNickname());
    System.out.println(); // Empty line
}

As an additional answer, you could also continue if null instead of break.
If there are elements in the array that do not point to anything and some of the elements after that, do point to values, this may be useful:

for (x = 0; x < 100; x++){
    if (name[x] == null)
        continue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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