简体   繁体   中英

Generating sequence numbers in Java from the ArrayList

How to generate sequence numbers and assign them to each object in java?

for example i have the following,

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;
    }
}

in the above class, i have assigned values to all the objects for Sequence Number and if I remove any object from the list then the Sequence Number has to be re generated.

how to do this in java, would some one help me on this please?.

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;
    }

I this this should be a function to remove the object from the list.

You iterate the list and set the sequence number.

Be aware that your constructor is not assigning the sequence number, so although you provided values, they are all null . If you changed type to the more sensible int , they would be 0 .

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

try this way

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);
}

Add Element and Remove Element methods

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();
    }
}

constructor

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM