在Java中可以给索引值命名吗?

huangapple go评论82阅读模式
英文:

Can I give names to index values in Java

问题

可以,您可以使用Java中的二维数组或者自定义对象的数组来实现这个目标。通过使用自定义对象的数组,您可以为每个元素添加标识符,从而更容易管理和访问员工数据。以下是一个示例:

// 定义一个Employee类
class Employee {
    private int empID;
    private String empFName;
    private int empAge;
    private double empSalary;

    public Employee(int empID, String empFName, int empAge, double empSalary) {
        this.empID = empID;
        this.empFName = empFName;
        this.empAge = empAge;
        this.empSalary = empSalary;
    }

    // 添加getter和setter方法
    public int getEmpID() {
        return empID;
    }

    public void setEmpID(int empID) {
        this.empID = empID;
    }

    public String getEmpFName() {
        return empFName;
    }

    public void setEmpFName(String empFName) {
        this.empFName = empFName;
    }

    public int getEmpAge() {
        return empAge;
    }

    public void setEmpAge(int empAge) {
        this.empAge = empAge;
    }

    public double getEmpSalary() {
        return empSalary;
    }

    public void setEmpSalary(double empSalary) {
        this.empSalary = empSalary;
    }
}

public class Main {
    public static void main(String[] args) {
        // 创建一个包含Employee对象的数组
        Employee[] employees = new Employee[2];
        employees[0] = new Employee(101, "Dave", 35, 50000);
        employees[1] = new Employee(102, "Alice", 28, 45000);

        // 访问和打印员工数据
        System.out.println("Employee1: " + employees[0].getEmpID() + ", " +
                employees[0].getEmpFName() + ", " +
                employees[0].getEmpAge() + ", " +
                employees[0].getEmpSalary());

        System.out.println("Employee2: " + employees[1].getEmpID() + ", " +
                employees[1].getEmpFName() + ", " +
                employees[1].getEmpAge() + ", " +
                employees[1].getEmpSalary());
    }
}

这段代码创建了一个Employee类,每个员工的信息都被存储在一个Employee对象中,然后将这些对象存储在一个数组中,每个员工都有一个唯一的标识符(例如,Employee1,Employee2)。这样,您可以轻松地访问和管理员工数据。

英文:

Say I have an array holding employee data:

[empID, empFName, empAge, empSalary, ...]

rather than each index being [0,1,...] can I instead give the index an identifier? I'm trying to create a loop where I store an array object [employee] inside of an array, with each index of the employee array holding one of the values above empID, empFName, ...,

What I'm trying to end up with is something like this:
[Employee1[101, Dave, 35, 50000], Employee2[...]...]

is this possible in Java?

答案1

得分: 1

Sure. Embrace what java is, which is a nominally typed system. Emphasis on nominal: Things are supposed to have names. Thus:

@Value
public class Employee {
    int id;
    String name;
    int age;
    long salary;
}

List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(101, "Dave", 35, 50000));

Some notes:

  1. arrays are low level constructs you generally shouldn't be using unless you really know you need them, or its component type is primitive (int[] is much harder to avoid). Use Lists. These can shrink and grow and have funcioning implementations of equals and hashCode and the like.

  2. You need to set up that class to have a constructor and such. If you're on java 14, you can use records; alternatively, you can use lombok's @Value, as in this example.

英文:

Sure. Embrace what java is, which is a nominally typed system. Emphasis on nominal: Things are supposed to have names. Thus:

@Value
public class Employee {
int id;
String name;
int age;
long salary;
}
List&lt;Employee&gt; employees = new ArrayList&lt;Employee&gt;();
employees.add(new Employee(101, &quot;Dave&quot;, 35, 50000));

Some notes:

  1. arrays are low level constructs you generally shouldn't be using unless you really know you need them, or its component type is primitive (int[] is much harder to avoid). Use Lists. These can shrink and grow and have funcioning implementations of equals and hashCode and the like.

  2. You need to set up that class to have a constructor and such. If you're on java 14, you can use records; alternatively, you can use lombok's @Value, as in this example.

答案2

得分: 0

从我理解的情况来看,你基本上在描述一个二维数组。虽然这是可能的,可以帮助你达到你想要的目标,但我认为如果你创建一个员工对象并将它们存储在一个ArrayList中,会更容易并且会教给你更多东西!然后,你可以使用arraylist.get(i).someEmployeeMethod()来访问你想要的信息,其中someEmployeeMethod()可以是像getName()或getId()这样的方法。如果你不知道什么是对象,你肯定需要了解一下。

英文:

From what I'm understanding you're essentially describing a 2D array. While that's possible and could get you to where you want to go, I think it'd be a lot easier and teach you a lot more if you created an employee object and stored them in an arraylist! Then, you'd access the info you want with arraylist.get(i).someEmployeeMethod() where someEmployeeMethod() could be something like getName() or getId(). If you don't know what an object is, you will definitely need to.

huangapple
  • 本文由 发表于 2020年7月22日 09:51:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63025584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定