Java 如何将适当的成本应用于排名的选择

Java 如何将适当的成本应用于排名的选择,java,arraylist,ranking,Java,Arraylist,Ranking,我有4个活动选择的列表 Activities Activity 1 Activity 2 Activity 3 Activity 4 我有一份选择了4项活动的人员名单,按顺序排列,例如: Person Choices (1st,2nd,3rd,4th) Person 1 2,3,1,4 Person 2 3,1,4,2 ... 我正试图根据他们的职位将成本分配给排名靠前的选择,我只是想知道我如何才能做到这一点。 例如,人员1的第一选择是活动2,其成本为1。他们的第二个选择是活动3,

我有4个活动选择的列表

Activities
Activity 1
Activity 2
Activity 3
Activity 4
我有一份选择了4项活动的人员名单,按顺序排列,例如:

Person    Choices (1st,2nd,3rd,4th)
Person 1  2,3,1,4
Person 2  3,1,4,2
...
我正试图根据他们的职位将成本分配给排名靠前的选择,我只是想知道我如何才能做到这一点。 例如,人员1的第一选择是活动2,其成本为1。他们的第二个选择是活动3,成本是2,因为它位于位置2,依此类推。 我将这些成本添加到列表中,因为我以后需要以相同的顺序添加此成本列表

我试过的代码

List<Integer> cost = new ArrayList<Integer>();
for(Person p: people){
 for (int i = 0; i < p.getChoices().size(); i++) {

  cost.add(p.getChoices(i+1);
 }
}

我一直对如何应用它感到困惑,因为它应该很简单。在我以前的实现中,我最终得到的成本列表是1,2,3,4。由于某种原因,我无法理解这个理论(因为它是凌晨5点)。 我对任何理论或伪代码都持开放态度。
提前谢谢你

我正在编写一个示例代码。根据我对你问题的理解,我已经为你写了一段代码

人类

import java.util.ArrayList;

public class Person {
    ArrayList<Integer> choices = new ArrayList<>();

    public ArrayList<Integer> getChoices() {
       return choices;
    }

    public void setChoices(ArrayList<Integer> choices) {
       this.choices = choices;
    }
}
import java.util.ArrayList;
公共阶层人士{
ArrayList选项=新建ArrayList();
公共ArrayList getChoices(){
返回选择;
}
公共void集合选项(ArrayList选项){
这个。选择=选择;
}
}
主要方法

public class MainMethod {

    public static void main(String[] args) {

       Person person1 = new Person();
       ArrayList<Integer> activity1 = new ArrayList<>();
       activity1.add(2);
       activity1.add(3);
       activity1.add(1);
       activity1.add(4);
       person1.setChoices(activity1);

       Person person2 = new Person();
       ArrayList<Integer> activity2 = new ArrayList<>();
       activity2.add(2);
       activity2.add(4);
       activity2.add(1);
       activity2.add(3);
       person2.setChoices(activity2);

       Person person3 = new Person();
       ArrayList<Integer> activity3 = new ArrayList<>();
       activity3.add(1);
       activity3.add(3);
       activity3.add(4);
       activity3.add(2);
       person3.setChoices(activity3);

       Person person4 = new Person();
       ArrayList<Integer> activity4 = new ArrayList<>();
       activity4.add(4);
       activity4.add(3);
       activity4.add(1);
       activity4.add(4);
       person4.setChoices(activity4);

       ArrayList<Person> persons = new ArrayList<>();
       persons.add(person1);
       persons.add(person2);
       persons.add(person3);
       persons.add(person4);

       int i = 1;
       for(Person person :persons) {
           System.out.println("Person"+i);
           //List
           person.getChoices().forEach(System.out::println);
           i++;
       }
       System.out.println("**********************");
       //If you need cost list. I gave you option to store in list as well.
       int j = 1;
       for(Person person :persons) {
        System.out.println("Person"+j);
        ArrayList<Integer> cost = person.getChoices();
        cost.stream().forEach(System.out::println);
        i++;
       }
   }
}
公共类main方法{
公共静态void main(字符串[]args){
Person person1=新的Person();
ArrayList activity1=新建ArrayList();
活动1.添加(2);
活动1.添加(3);
活动1.添加(1);
活动1.添加(4);
person1.setChoices(activity1);
Person person2=新的Person();
ArrayList activity2=新建ArrayList();
活动2.添加(2);
活动2.添加(4);
活动2.添加(1);
活动2.添加(3);
人员2.设置选项(活动2);
Person person3=新的Person();
ArrayList activity3=新建ArrayList();
活动3.添加(1);
活动3.添加(3);
活动3.添加(4);
活动3.添加(2);
人员3.设置选项(活动3);
Person person4=新人();
ArrayList activity4=新建ArrayList();
活动4.添加(4);
活动4.添加(3);
活动4.添加(1);
活动4.添加(4);
人4.设置选择(活动4);
ArrayList persons=新建ArrayList();
人员。添加(人员1);
人员。添加(人员2);
人员。添加(人员3);
人员。添加(人员4);
int i=1;
用于(人:人){
系统输出打印项次(“人”+i);
//名单
person.getChoices().forEach(System.out::println);
i++;
}
System.out.println(“*************************”);
//若你们需要成本清单,我给你们选择存储在清单中。
int j=1;
用于(人:人){
系统输出打印项次(“人”+j);
ArrayList成本=person.getChoices();
cost.stream().forEach(System.out::println);
i++;
}
}
}

如果您的要求不同,请详细说明并让我知道。

我正在编写示例代码。根据我对你问题的理解,我已经为你写了一段代码

人类

import java.util.ArrayList;

public class Person {
    ArrayList<Integer> choices = new ArrayList<>();

    public ArrayList<Integer> getChoices() {
       return choices;
    }

    public void setChoices(ArrayList<Integer> choices) {
       this.choices = choices;
    }
}
import java.util.ArrayList;
公共阶层人士{
ArrayList选项=新建ArrayList();
公共ArrayList getChoices(){
返回选择;
}
公共void集合选项(ArrayList选项){
这个。选择=选择;
}
}
主要方法

public class MainMethod {

    public static void main(String[] args) {

       Person person1 = new Person();
       ArrayList<Integer> activity1 = new ArrayList<>();
       activity1.add(2);
       activity1.add(3);
       activity1.add(1);
       activity1.add(4);
       person1.setChoices(activity1);

       Person person2 = new Person();
       ArrayList<Integer> activity2 = new ArrayList<>();
       activity2.add(2);
       activity2.add(4);
       activity2.add(1);
       activity2.add(3);
       person2.setChoices(activity2);

       Person person3 = new Person();
       ArrayList<Integer> activity3 = new ArrayList<>();
       activity3.add(1);
       activity3.add(3);
       activity3.add(4);
       activity3.add(2);
       person3.setChoices(activity3);

       Person person4 = new Person();
       ArrayList<Integer> activity4 = new ArrayList<>();
       activity4.add(4);
       activity4.add(3);
       activity4.add(1);
       activity4.add(4);
       person4.setChoices(activity4);

       ArrayList<Person> persons = new ArrayList<>();
       persons.add(person1);
       persons.add(person2);
       persons.add(person3);
       persons.add(person4);

       int i = 1;
       for(Person person :persons) {
           System.out.println("Person"+i);
           //List
           person.getChoices().forEach(System.out::println);
           i++;
       }
       System.out.println("**********************");
       //If you need cost list. I gave you option to store in list as well.
       int j = 1;
       for(Person person :persons) {
        System.out.println("Person"+j);
        ArrayList<Integer> cost = person.getChoices();
        cost.stream().forEach(System.out::println);
        i++;
       }
   }
}
公共类main方法{
公共静态void main(字符串[]args){
Person person1=新的Person();
ArrayList activity1=新建ArrayList();
活动1.添加(2);
活动1.添加(3);
活动1.添加(1);
活动1.添加(4);
person1.setChoices(activity1);
Person person2=新的Person();
ArrayList activity2=新建ArrayList();
活动2.添加(2);
活动2.添加(4);
活动2.添加(1);
活动2.添加(3);
人员2.设置选项(活动2);
Person person3=新的Person();
ArrayList activity3=新建ArrayList();
活动3.添加(1);
活动3.添加(3);
活动3.添加(4);
活动3.添加(2);
人员3.设置选项(活动3);
Person person4=新人();
ArrayList activity4=新建ArrayList();
活动4.添加(4);
活动4.添加(3);
活动4.添加(1);
活动4.添加(4);
人4.设置选择(活动4);
ArrayList persons=新建ArrayList();
人员。添加(人员1);
人员。添加(人员2);
人员。添加(人员3);
人员。添加(人员4);
int i=1;
用于(人:人){
系统输出打印项次(“人”+i);
//名单
person.getChoices().forEach(System.out::println);
i++;
}
System.out.println(“*************************”);
//若你们需要成本清单,我给你们选择存储在清单中。
int j=1;
用于(人:人){
系统输出打印项次(“人”+j);
ArrayList成本=person.getChoices();
cost.stream().forEach(System.out::println);
i++;
}
}
}

如果您的要求不同,请详细说明并让我知道。

如果一个人的活动成本是其在选项中的位置,您可以在课堂上声明一种方法
costOf

public class Person {
    private final List<Integer> choices = new ArrayList<>();

    public Person(Integer... choices) {
        this.choices.addAll(Arrays.asList(choices));
    }

    public List<Integer> getChoices() {
       return choices;
    }

    public int costOf(Integer activity) {
        return choices.indexOf(activity)+1;
    }
}
你可以看到有一个零,因为第二个人的选择中没有2

假设是打字错误,将第二个1替换为2,则得到:

1 3 2
2 1 4
3 2 1
4 4 3

所期望的。

如果一个人的活动成本是其在选项中的位置,您可以在类person中声明方法
costOf

public class Person {
    private final List<Integer> choices = new ArrayList<>();

    public Person(Integer... choices) {
        this.choices.addAll(Arrays.asList(choices));
    }

    public List<Integer> getChoices() {
       return choices;
    }

    public int costOf(Integer activity) {
        return choices.indexOf(activity)+1;
    }
}
你可以看到有一个零,因为第二个人的选择中没有2

假设是打字错误,将第二个1替换为2,则得到:

1 3 2
2 1 4
3 2 1
4 4 3

这是意料之中的事。

我愿意向你展示你迄今为止所做的尝试。这是一件小事,但它阻止了我与其他人的融合