java - 显示另一个类的多个数组列表

标签 java arrays

在我们当前的章节中,我们正在使用数组,但我在创建要从另一个类调用的列表时遇到了一些麻烦。

目标:显示另一个类的并行数组,可以是单个数组,也可以是一组数组。

问题:调用具有不同数据类型的多并行数组的最佳或有效方法?

错误:以非法语句开头,如前所述,这里是整个代码,请忽略我刚刚测试以确保数组设置正确的循环显示。

谢谢大家,非常感谢您的帮助

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/

相关文章:

javascript - 为什么这个 javascript 代码不起作用?

java - Lombok 的构造函数功能

java - 在java中用日文字符声明字符串

javascript - 如何在 AngularJS 中添加/删除字符串数组

C++:构造函数中的多维数组初始化

arrays - vba,为什么无维数组与 Split() 一起使用

java - 在小程序 HTML 上加载图像

java - 除了 Apache Math Mean 之外,深层含义是什么?请参阅代码

java - 从 recyclerview 访问某个按钮并更改其背景图像

javascript - 无论输入如何,函数都返回 true