【问题标题】:How to sort list from least to greatest [duplicate]如何从最小到最大对列表进行排序[重复]
【发布时间】:2015-01-30 18:38:22
【问题描述】:

这个程序差不多完成了,但是当它被整理出来时,权重是按照从小到大的顺序排列的,这是正确的。唯一的问题是与体重相关的姓名和年龄没有按体重排序。

例如

迈克 25 180
仁 36 105
山姆 22 120

我应该得到

仁 36 105
山姆 22 120
迈克 25 180

但我明白了

迈克 25 105
仁 36 120
山姆 22 180
import java.util.*;
import java.util.ArrayList;

import java.util.List;

public class Lab0 {


public static void main(String[] args) {

  //Scanner and variables  
  Scanner keyboard = new Scanner (System.in);
  String name = "";
  int age;
  double weight;
  int number;

  //Creates list for name, age, and weight
  ArrayList<String> Name = new ArrayList<String>();
  ArrayList<Integer> Age = new ArrayList<Integer>();
  ArrayList<Double> Weight = new ArrayList<Double>();

    System.out.println("Enter the information needed.");

    //Ask user to enter information needed
    while (true){
        System.out.print("Enter last name: ");
        name = keyboard.nextLine();

        if(name.equalsIgnoreCase("FINISHED")){
            break;
        }
        else {
            Name.add(name);
            System.out.print("Enter age: "); 
            age = keyboard.nextInt();
            Age.add(age);
            System.out.print("Enter weight: ");
            weight = keyboard.nextDouble();
            Weight.add(weight);
            keyboard.nextLine();
            System.out.println("==========================================\n");

        }
    }

    //new list to sort weight and age and name
    ArrayList<String> NameList = Name;
    ArrayList<Integer> AgeList = Age;
    ArrayList<Double> WeightList = Weight;

    Collections.sort(Weight);
    for(int i=0; i<Weight.size(); i++){
        for (int j=0; j<Weight.size(); j++){
            if(WeightList.get(j) == Weight.get(i)){
                Name.set(j, NameList.get(i));
                Age.set(j, AgeList.get(i));
            }

            else;
        }
    }



    //prints out information entered
     for (int k=0; k<Weight.size(); k++){

         System.out.println("Name: " + Name.get(k) + " Age: " + Age.get(k)
         + " weight: " + Weight.get(k));
     }


     while (true){
         System.out.println("=============================================");
         System.out.print("Enter a last name that you listed: ");
         String Search = keyboard.next();

         int index = Name.indexOf(Search);
         if (index >=0){
             System.out.println("Age: " + Age.get(index));
             System.out.println("Weight: " + Weight.get(index));
         }
         else if(Search.equalsIgnoreCase ("DONE")){
             System.exit(0);
         }
         else{
             System.out.println("NOT FOUND");
         }
     }
}

}

【问题讨论】:

  • 为什么不创建一个 Person 对象并创建一个 Comparable 方法?
  • 您应该准确地说明作业是什么。就个人而言,我会创建一个“Person”对象列表并对其进行排序,而不是使用三个单独的列表,但这可能不是您的任务,所以请告诉我们。
  • 我们必须使用一个数组进行赋值来列出提供的信息。
  • 你可能想和你的教授谈谈,问一些澄清问题。一个合理的解决方案是创建一个封装 Person 的类(如 @ChrisForrence 和 @RealSkeptic 所建议的那样)。该解决方案仍将使用一个数组,只有一个数组而不是三个。
  • 您可能想访问this资源;它很好地解释了对象的排序。

标签: java arrays collections


【解决方案1】:

问题是,单个信息之间的关系正在丢失。 一个人是三胞胎(姓名、年龄、体重)。

您有三个列表,在使用权重对列表进行排序后,您想要连接单个信息。

这是你代码的问题部分:

ArrayList<String> NameList = Name;
ArrayList<Integer> AgeList = Age;
ArrayList<Double> WeightList = Weight;

Collections.sort(Weight);
for(int i=0; i<Weight.size(); i++){
    for (int j=0; j<Weight.size(); j++){
        if(WeightList.get(j) == Weight.get(i)){
            Name.set(j, NameList.get(i));
            Age.set(j, AgeList.get(i));
        }

        else;
    }
}

你的第一个问题是: 如果两个或两个以上的人体重相同,你就找不到明确的关系。 (不是您问题的主题,而是一个问题。)

你的第二个问题是: (如果所有重量都不同。)

Collections.sort(Weight);

此方法对列表“重量”进行排序。 但它与名称为“WeightList”的列表相同。 您正在复制参考。

你应该先克隆列表,然后再使用排序。

而且你需要改变'Name.set(...)'和'Age.set(...)'中的i和j。

这应该会有所帮助:

ArrayList<String> NameList = (ArrayList<String>) Name.clone();
ArrayList<Integer> AgeList = (ArrayList<Integer>) Age.clone();
ArrayList<Double> WeightList = (ArrayList<Double>) Weight.clone();

Collections.sort(Weight);
for (int i = 0; i < Weight.size(); i++) {
    for (int j = 0; j < Weight.size(); j++) {
        if (WeightList.get(j) == Weight.get(i)) {
            Name.set(i, NameList.get(j));
            Age.set(i, AgeList.get(j));
        }

        else
            ;
    }
}

我认为这就是你的问题的答案。

另外:

您应该考虑您的问题的“ChrisForrence”评论。 最好为实现“Comparable”接口的人使用一个类。

在我看来,您应该使用比较器。 这是获得灵活性的一般方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 2016-10-31
    • 2014-04-26
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    相关资源
    最近更新 更多