在我们当前的章节中,我们正在使用数组,但我在创建要从另一个类调用的列表时遇到了一些麻烦。
目标:显示另一个类的并行数组,可以是单个数组,也可以是一组数组。
问题:调用具有不同数据类型的多并行数组的最佳或有效方法?
错误:以非法语句开头,如前所述,这里是整个代码,请忽略我刚刚测试以确保数组设置正确的循环显示。
谢谢大家,非常感谢您的帮助
import java.util.ArrayList;
public class Employee {
public static void main(String[] args) {
// create an array with employee number, first name, last name, wage, and Skill
int[] empID = {1001, 1002, 1003};
String[] firstName = {"Barry", "Bruce", "Selina"};
String[] lastName = {"Allen", "Wayne", "Kyle"};
double[] wage = {10.45, 22.50, 18.20};
String[] skill = {"Delivery Specialist", "Crime Prevention", "Feline Therapist"};
/*
for ( int i = 0; i < empID.length; i++ )
{
System.out.print( "Employee ID: " + empID[i] + "\n");
System.out.print( "First Name: " + firstName[i] + "\n");
System.out.print( "Last Name: " + lastName[i] + "\n");
System.out.print( "Hourly Wage: $" + wage[i] + "\n");
System.out.print( "Skill: " +skill[i] );
System.out.println("\n");
}
*/
//create an object to be called upon from another class
public ArrayList<int, String, String, double, String> getEmployee() {
ArrayList<int, String, String, double, String> employeeList = new ArrayList<int, String, String, double, String>();
employeeList.add(empID);
employeeList.add(firstName);
employeeList.add(lastName);
employeeList.add(wage);
employeeList.add(skill);
return employeeList;
}
}
} //end of class
最佳答案
首先,你不能像这样声明 ArrayList:
ArrayList<int, String, String, double, String>
如果你想要的话,你可以创建自己的对象,创建一个可以接受这些值的类,然后你可以创建这个对象的ArrayList,例如:
class MyClass{
int att1;
String att2;
String att3;
double att4;
String att5;
public MyClass(int att1, String att2, String att3, double att4, String att5) {
this.att1 = att1;
this.att2 = att2;
this.att3 = att3;
this.att4 = att4;
this.att5 = att5;
}
}
然后你可以像这样创建一个ArrayList:
List<MyClass> list = new ArrayList<>();
关于java - 显示另一个类的多个数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43559605/