Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将列表从最小到最大排序_Java_Arrays_Collections - Fatal编程技术网

Java 如何将列表从最小到最大排序

Java 如何将列表从最小到最大排序,java,arrays,collections,Java,Arrays,Collections,这个程序几乎完成了,但是当它被整理出来时,权重被安排为从最小到最大,这是正确的。唯一的问题是,与体重相关联的姓名和年龄没有按照体重排序 比如说 mike 25 180 jen 36 105 sam 22 120 我应该 jen 36 105 sam 22 120 mike 25 180 但我明白了 mike 25 105 jen 36 120 sam 22 180 问题是,单一信息之间的关系正在松动。 一个人的名字、年

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

比如说

mike 25 180 jen 36 105 sam 22 120 我应该

jen 36 105 sam 22 120 mike 25 180 但我明白了

mike 25 105 jen 36 120 sam 22 180
问题是,单一信息之间的关系正在松动。 一个人的名字、年龄、体重都是三重的

您有三个列表,在使用权重对列表进行排序后,您希望将单个信息连接起来

这就是代码中的问题部分:

此方法将列表排序为“权重”。 但它是同一个名为“权重列表”的列表。 你在复制参考资料

你应该先克隆列表,因为你要使用排序

你需要改变'Name.set…'和'Age.set…'中的i和j

这将有助于:

我想这就是你问题的答案

此外:

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

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

为什么不将Person设为对象并设置一个可比较的方法呢?您可能应该确切地说明任务是什么。就我个人而言,我会创建一个Person对象列表并对其进行排序,而不是使用三个单独的列表,但这可能不是您的作业所涉及的内容,因此请让我们知道。我们必须为作业使用数组来列出所提供的信息。您可能需要与教授交谈并提出一些澄清问题。一个合理的解决方案是创建一个类来封装@ChrisForrence和@realponsign建议的Person。解决方案仍然使用一个数组,只有一个数组而不是三个;它很好地解释了排序对象。
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");
         }
     }
}

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