为我的对象创建唯一标识符

3

我有Java的基础知识,想要为我的员工创建独特的ID,但我不想使用java.util.UUID。我该怎么做?在哪里以及应该向我的代码添加什么方法?谢谢。

import java.util.ArrayList;

public class Main {

    private static ArrayList<Employees> list = new ArrayList<>();

    public static void main(String[] args) {
        Employees emp =new Employees(15, "xx", 23);
        Main.add(emp);        
    }

    public static void delete(int id) {
        list.remove(get(id));
    }

    public static void updateName(int id, String name) {
        get(id).name=name;
    }

    public static void updateAge(int id, int age) {
        get(id).age=age;
    }

    public static Employees get(int id) {
        for(Employees emp : list)
            if(emp.id==id)
                return emp;
        throw new RuntimeException("Employees with id : "+id+" not found");
    }

}
class Employees {

    String name;
    int age;
    int id ;

    public Employees(int id, String name, int age) {
        //super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

   @Override
    public String toString() {

        return id+" : "+" "+name+", "+age+" ans";
    }
}

Main.add() 你确定吗?^^ "EmployeeS" 带 s 吗?一个简单的方式是在 Employees 类中使用静态计数器作为 ID 并递增它。 - azro
UUID 意味着“通用唯一标识符”...所以它是一个 ID...为什么你不想使用它呢? - ΦXocę 웃 Пepeúpa ツ
get 函数中,我建议不要使用 _RuntimeException_,而是使用 NoSuchElementException - sandromark78
2个回答

6
你可以使用静态变量来存储一个ID,在赋值后将其递增,以确保下一个ID不同:
class Employees {

    String name;
    int age;
    int id ;
    static int counter = 0;

    public Employees(String name, int age) {
        this.id = counter++;
        this.name = name;
        this.age = age;
    }
  }

因此,您可以在构造函数中删除int id


另外:

  • Main.add(emp)是错误的,应该替换为list.add(emp)
  • updateNameupdateAge可以使用Employees中定义的setters来遵循惯例(更好地分离类,并将属性可见性设置为私有)

@M.Maria 不客气,您可以考虑点赞或更好的选择是:接受答案 ;) - azro
对于delete()方法,它不接受id作为参数,我应该用其他东西来替换它吗? - M.Maria
公共静态无返回值的删除方法,参数为id: { list.remove(get(id)); } - azro

0
嘿,尝试使用Java类中的Random

import java.util.ArrayList; import java.util.Random;

public class EmpUniqueId {

    public static void main(String[] args) {

         int empid1 =generateDummySSNNumber();
        Employees emp1 = new Employees(empid1 , "xx", 23);

        int empid2 =generateDummySSNNumber();
        Employees emp2 = new Employees(empid2, "xx", 23);
         ArrayList<Employees> list = new ArrayList();
         list.add(emp1);
         list.add(emp2);
         for (Employees object: list) {
                System.out.println("Employees id is :-"+object.getId());
            }
    }

    public static int generateDummySSNNumber() {
        Random random = new Random();
        int min, max;
        min = 1;
        max = 2000;
        int num = random.nextInt(min);
        int num1 = random.nextInt(max);
        System.out.println("Random Number between given range is " + num1);


        return num1;
    }
}



class Employees {

    String name;
    int age;
    int id ;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Employees(int id, String name, int age) {
        //super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

   @Override
    public String toString() {

        return id+" : "+" "+name+", "+age+" ans";
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接