change one Array list item order relative to another

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

change one Array list item order relative to another

问题

Ive got 2 Array lists

ArrayList<String> passengerNames = new ArrayList<String>();//stores passenger names
ArrayList<String> seatNumber = new ArrayList<String>();//stores the seat passenger booked 1  seat=1passenger
passengerNames.add("Ram");
passengerNames.add("Shyam");
passengerNames.add("CPS");
passengerNames.add("John");
passengerNames.add("Steve");

seatNumber.add(1);
seatNumber.add(84);
seatNumber.add(19);
seatNumber.add(99);
seatNumber.add(78);

so the passengerNames array will be
[Ram, Shyam, CPS, John, Steve]
and seatNumber array will be 
[1, 84, 19, 99, 78]

I want to sort the seatNumber array in ascending order but I want to change the passengerNames list with it, so when seatNumber is in ascending order like
[1, 19, 78, 84, 99]
the passengerNames array should be sorted like
[Ram, CPS, Steve, Shyam, John]
英文:

Ive got 2 Array lists

 ArrayList&lt;String&gt; passengerNames= new ArrayList&lt;String&gt;();//stores passenger names
ArrayList&lt;String&gt; seatNumber= new ArrayList&lt;String&gt;();//stores the seat passenger booked 1  seat=1passenger
    passengerNames.add(&quot;Ram&quot;);
    passengerNames.add(&quot;Shyam&quot;);
    passengerNames.add(&quot;CPS&quot;);
    passengerNames.add(&quot;John&quot;);
    passengerNames.add(&quot;Steve&quot;);
    
    seatNumber.add(1);
    seatNumber.add(84);
    seatNumber.add(19);
    seatNumber.add(99);
    seatNumber.add(78);

so the passengerNames array will be
[Ram, Shyam, CPS, John, Steve]
and seatNumber array will be
[1, 84, 19, 99, 78]
I want to sort the seatNumber array in accending order but I want to change the passengerNames list with it as in when seatNumber is in accending order like
[1,19,78,84,99]
the passengerNames array should be sorted like
[Ram,CPS,Steve,Shyam,Jhon]

答案1

得分: 3

我建议将您的数据封装在一个新的Passenger对象中,并创建一个类型为Passenger的单个ArrayList - 这样可以保持整洁和灵活。

public class Passenger {
    private String name;
    private int seatNumber;
    
    public Passenger(String name, int seatNumber) {
        this.name = name;
        this.seatNumber = seatNumber;
    }
}

... 并且用于创建Passenger ArrayList 的部分:

List<Passenger> passengers = new ArrayList<Passenger>(); // 构建Passenger ArrayList
passengers.add(new Passenger("Ram", 1)); // 创建新的Passenger
passengers.add(new Passenger("Shyam", 84)); // 等等...

关于按升序对List进行排序,一个清晰的方法是在Passenger类中实现Comparable接口,并在compareTo(Passenger p)方法中定义行为。一旦按您的喜好定义了该方法,可以通过Collections.sort(passengers); 调用排序。

英文:

I would suggest encapsulating your data in a new Passenger object and creating a single ArrayList of type Passenger - this should keep things tidy and flexible.

public class Passenger {
    private String name;
    private int seatNumber;
    
    public Passenger(String name, int seatNumber) {
        this.name = name;
        this.seatNumber = seatNumber;
    }
}

... and for creating the Passenger ArrayList

List&lt;Passenger&gt; passengers = new ArrayList&lt;Passenger&gt;(); // build Passenger ArrayList
passengers.add(new Passenger(&quot;Ram&quot;, 1)); // create new Passenger
passengers.add(new Passenger(&quot;Shyam&quot;, 84)); // etc...

In regards to sorting the List in ascending order, a clean approach is to implement Comparable in the Passenger class, and define the behavior in the compareTo(Passenger p) method. Once that is defined to your liking, the sort can be called via Collections.sort(passengers);

答案2

得分: 1

代替拥有属于单个实体的两个信息数组,创建一个名为 "Passenger" 的对象,其中你可以赋予姓名和座位号。

public class Passenger {
 
    private String name;
    private int seatNumber;

    public Passenger(String name, int seatNumber){
        this.name = name;
        this.seatNumber = seatNumber;
    }
    // 设置器和获取器(setters and getters)
}

现在只需创建您的乘客数组。

ArrayList<Passenger> passengers = new ArrayList<Passenger>();
passengers.add(new Passenger("Ram", 1));

然后使用按对象属性排序的方法。

Arrays.sort(passengers, Comparator.comparing(Passenger::getSeatNumber));
英文:

Instead of having two arrays of information that belong to a single entity, create an Object Passenger in which you assign the name and seat number.

public class Passenger {

    private String name;
    private int seatNumber;

    public Passenger(String name, int seatNumber){
        this.name = name;
        this.seatNumber = seatNumber;
    }
    // setters and getters
}

Now just create your array of passengers

ArrayList&lt;Passenger&gt; passengers = new ArrayList&lt;Passenger&gt;();
passengers.add(new Passenger(&quot;Ram&quot;, 1));

Then use a sorting method by Object attribute.

Arrays.sort(passengers, Comparator.comparing(Passenger::getSeatNumber));

答案3

得分: 0

看起来使用SortedMap会更合适

SortedMap<Integer, String> map = new TreeMap<>();
map.put(1, "Ram");
map.put(84, "Shyam");
map.put(19, "CPS");
map.put(99, "John");
map.put(78, "Steve");

在你的情况下,你不需要在构造函数中传递比较器,因为你需要键按升序排列,即它们的自然顺序。很酷的一点是,它们会在不做任何操作的情况下自动排序,当你添加它们时它们会自动排序。

英文:

Looks like it would be more appropriate to use a SortedMap

SortedMap&lt;Integer, String&gt; map = new TreeMap&lt;&gt;();
map.put(1, &quot;Ram&quot;);
map.put(84, &quot;Shyam&quot;);
map.put(19, &quot;CPS&quot;);
map.put(99, &quot;John&quot;);
map.put(78, &quot;Steve&quot;);

You don't need to pass a Comparator in the constructor in your case, because you need the keys to be in ascendent order, that is their natural order. The cool thing is that they will be already sorted without doing anything, they will sort automatically as you add them

huangapple
  • 本文由 发表于 2020年4月6日 17:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61056707.html
匿名

发表评论

匿名网友

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

确定