从ArrayList生成Java中的序列号

时间:2015-09-28 05:47:30

标签: java arraylist sequence

如何生成序列号并将它们分配给java中的每个对象?

例如我有以下内容,

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class MyMaxUDOComparable
{

    public static Integer findMaxScore(List<testVO> emps)
    {
        Integer maxScoreTotal = 0;

        for (Iterator<JobFitSurveyConfigVO> iterator = emps.iterator(); iterator.hasNext();)
        {
            testVOempl = (testVO) iterator.next();

            if (empl != null)
            {
                maxScoreTotal += empl.getSalary();
            }

        }
        return maxScoreTotal;
    }

    public static void main(String a[])
    {

        List<testVO> emps = new ArrayList<testVO>();
        emps.add(new testVO(10, "Raghu", 10,1));
        emps.add(new testVO(120, "Krish", 10,2));
        emps.add(new testVO(210, "John", 10,3));
        emps.add(new testVO(150, "Kishore", 10,4));
        testVOmaxSal = Collections.max(emps);

        System.out.println("Employee with max Id: " + maxSal);
        System.out.println("maxScoreTotal: " + findMaxScore(emps));
    }
}

class testVOimplements Comparable<testVO>
{

    private Integer id;
    private String name;
    private Integer salary;
    private Integer sequenceNumber;

    public testVO(Integer id, String name, Integer sal,Integer sequenceNumber) {
        this.id = id;
        this.name = name;
        this.salary = sal;
    }

    public Integer getId()
    {
        return id;
    }
    public void setId(Integer id)
    {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public Integer getSalary()
    {
        return salary;
    }
    public void setSalary(Integer salary)
    {
        this.salary = salary;
    }

    public Integer getSequenceNumber()
    {
        return sequenceNumber;
    }

    public void setSequenceNumber(Integer sequenceNumber)
    {
        this.sequenceNumber = sequenceNumber;
    }

    @Override
    public int compareTo(JobFitSurveyConfigVO emp)
    {

        return this.id.compareTo(emp.getId());
    }
    public String toString()
    {
        return id + "  " + name + "   " + salary;
    }
}

在上面的类中,我为Sequence Number的所有对象分配了值,如果从列表中删除任何对象,则必须重新生成Sequence Number

如何在java中执行此操作,有人会帮我这个吗?。

3 个答案:

答案 0 :(得分:1)

public void deleteObjFormList(JobFitSurveyConfigVO emp,List<JobFitSurveyConfigVO> emps)
    {
        int i = emps.indexOf(emp);
        emps.remove(i);
        for(int j=i;j<emps.size();j++)
        {
            JobFitSurveyConfigVO emp1 = emps.get(j);
            emp1.setSequenceNumber(emp1.getSequenceNumber()-1);
        }
        return;
    }

我这应该是一个从列表中删除对象的函数。

答案 1 :(得分:0)

您迭代列表并设置序列号。

请注意,您的构造函数未分配序列号,因此尽管您提供了值,但它们都是null。如果您将类型更改为更明智的int,则它们将为0

// Renumber (aka resequence) the emp records
for (int i = 0; i < emps.size(); i++)
    emps.get(i).setSequenceNumber(i + 1);

答案 2 :(得分:0)

试试这种方式

public static void main(String a[])  {

List<JobFitSurveyConfigVO> emps = new ArrayList<JobFitSurveyConfigVO>();

    ArrayList<Integer> seq = new ArrayList<Integer>();
    addElement(seq, emps, 10, "Raghu", 10);
    addElement(seq, emps, 120, "Krish", 10);
    addElement(seq, emps, 210, "John", 10);
    addElement(seq, emps, 150, "Kishore", 10);

    System.out.println("Display : "+emps);

    removeElement(2, seq, emps);
    System.out.println("Removed : "+emps);

    addElement(seq, emps, 210, "John2", 10);
    System.out.println("Added : "+emps);
}

添加元素和删除元素方法

public static void addElement(ArrayList<Integer> seq, 
       List<JobFitSurveyConfigVO> emps, int id, String name, Integer salary)  {

     int size = seq.size();
     Collections.sort(seq); // Make sure they are in Sequence.
     if (size > 1) {
         for (int i = 1; i < size; i++) {
             int check = seq.get(i-1);
             if ( (check + 1) != (seq.get(i))) {
                 seq.add(check + 1);
                 emps.add(new JobFitSurveyConfigVO(id, name, salary, (check + 1)));
                 break;
             }
             if (i+1 == size) {
                 seq.add(seq.get(i) + 1);
                 emps.add(new JobFitSurveyConfigVO(id, name, salary, (seq.get(i) + 1)));
             }
         }
     }else if (size == 1 && seq.get(0) == 1) {
         int check = seq.get(0);
         seq.add(check + 1);
         emps.add(new JobFitSurveyConfigVO(id, name, salary, (check + 1)));
     }else{
         seq.add(1);
         emps.add(new JobFitSurveyConfigVO(id, name, salary, 1));
     }
}
public static void removeElement(int index, ArrayList<Integer> seq, List<JobFitSurveyConfigVO> emps){
    if (index < seq.size()) {
        emps.remove(index);
        seq.remove(index);
    }else {
        throw new ArrayIndexOutOfBoundsException();
    }
}

构造

public JobFitSurveyConfigVO(Integer id, String name, Integer sal,Integer sequenceNumber) {
    this.id = id;
    this.name = name;
    this.salary = sal;
    this.sequenceNumber = sequenceNumber;
}