我想让我的toString方法打印我的数组列表中对象的所有信息,但我得到的输出与[]括号



对于我的代码,我尝试使用toString方法,我不确定这是否是在数组列表中显示对象信息的最佳方式。我想尝试一种普通方法,然后调用它但我该如何对数组列表中创建的每个对象都这么做呢?有更好的方法来解决这个问题吗?这就是我到目前为止所拥有的,但是输出输出了我不想要的带有[]括号的信息。什么是正确的方式来显示对象的数组列表信息?如有任何帮助或建议,我将不胜感激。

EmailApp.java

package emailapp;
import java.util.*;
public class EmailApp 
{
public static void main(String[] args) 
{
String firstName;
String lastName;
int numEmployees;


ArrayList<Email> list = new ArrayList<>();

//user input
Scanner keyboard = new Scanner(System.in);
System.out.println("How many employees would you like to add?");

numEmployees = keyboard.nextInt();
keyboard.nextLine();      //consume newline left over after nextInt

while(numEmployees <=0 || numEmployees >= 1000)
{
System.out.println("Your number of employees is a little rediculous don't you think? Try again.");
numEmployees = keyboard.nextInt();
keyboard.nextLine();      //consume newline left over after nextInt
}


for(int i = 0; i < numEmployees; i++)
{
System.out.println("What is new users first name?: ");

firstName = keyboard.nextLine();

System.out.println("What is new users last name?: ");

lastName = keyboard.nextLine();

//create object
list.add(new Email(firstName, lastName));
System.out.println(list);
}


//If youd like you can mess around with the setAltEmail or changePass method
}



}

Email.java

package emailapp;
import java.util.Scanner;
public class Email 
{
//Attributes

private String firstName;
private String lastName;
private String department;
private String password;
private final int DEFAULTPASSWORDLENGTH = 10;
private String email;
private String alternateEmail;
private int mailboxCapacity = 500;
private String companySuffix = "company.com";


//constructor. for first name last name
public Email(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
//System.out.println("EMAIL CREATED: " + this.firstName + " " + this.lastName);

//Call a method asking for the department - return the department
this.department = setDepartment();

System.out.println("Department: " + this.department);

//Call a method that returns a random password
this.password = randomPassword(DEFAULTPASSWORDLENGTH);
System.out.println("Your password is: " + this.password);

//Combine elements to generate email
email = firstName.toLowerCase() + "." + lastName.toLowerCase() + "@" + department + "." + companySuffix;
System.out.println("Your email is: " + email);

}

//ask for the department
private String setDepartment()
{
System.out.println("Enter the users department: ");
System.out.println("Enter 1: For SalesnEnter 2: For Development:nEnter 3: for Accounting:nEnter 0: For none");
System.out.println("Enter Choice: ");
//User choice for department
Scanner keyboard = new Scanner(System.in);
int departmentChoice = keyboard.nextInt();

if(departmentChoice == 1)
{
return "sales";
}
else if(departmentChoice == 2)
{
return "development";
}
else if(departmentChoice == 3)
{
return "accounting";
}
else
{
return "none";
}

}

//generate a random password
private String randomPassword(int length)
{
String passwordSet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$&";
char[] password = new char[length];

for(int i =0; i < length; i++)
{
int rand = (int)(Math.random() * passwordSet.length()); //rand = number between 1 and passwordSet length String
password[i] = passwordSet.charAt(rand);                 //picks out a random char from string and assigns to each element in array

}
return new String (password);   //Returning a String instead of individual characters
}

//set the mailbox capacity
public void setMailBoxCapacity(int capacity)
{
this.mailboxCapacity = capacity;
}
//set the alternate email
public void setAlternateEmail(String altEmail)
{
this.alternateEmail = altEmail;
}
//change the password
public void changePassword(String password)
{
this.password = password;
}

public int getMailboxCapacity()
{
return mailboxCapacity;
}
public String getAlternateEmail()
{
return alternateEmail;
}
public String getPassword()
{
return password;
}

@Override
public String toString()
{
return "DISPLAY NAME: " + firstName + " " + lastName
+"COMPANY EMAIL: " + email +
"MAILBOX CAPACITY: " + mailboxCapacity + "mb";
}

}

在列表上调用println将调用" tostring ";方法。

你需要的是遍历列表并调用每个Email对象的toString。

有很多方法可以做到这一点,但我认为最简单的方法是使用List::forEach方法传递一个对println: 的引用。
list.forEach(System.out::println);

相关内容

  • 没有找到相关文章

最新更新