【问题标题】:Display the variable names of an array sort?显示数组排序的变量名?
【发布时间】:2015-12-16 04:16:03
【问题描述】:

** 这是我在 StackOverflow 上的第一篇文章,我只在高中编写代码,所以我绝不是专业人士。我其实是个新手XD。还需要注意的是,我使用 ReadyToProgramInJava 作为我的 IDE,所以我使用 HSA 控制台来编写我的程序。无论如何,继续我的问题。

我正在编写一个程序,该程序需要我对一个 int 数组进行排序,然后显示该数组中的最大值。我正在使用 array.sort 按升序排列变量,然后打印出数组的最大值。像这样的:

public class stackOverflowQuestion


Console c;
public stackOverflowQuestion ()
{
    c = new Console ();
}


public void Sorting ()
{
int obi = 10;
int luke = 4;
int vader =21;
int palpatine = 5;
    int [ ] people = {obi, luke, vader, palpatine};
  Arrays.sort(people);    
  c.println (people [people.length - 1]);
}


public static void main (String[] args)
{
    stackOverflowQuestion s = new stackOverflowQuestion ();
    s.Sorting ();
}

问题是这样的:当我打印数组的最大值时,它只显示数字。所以,如果我按原样运行这个程序,它会输出“21”。理想情况下,我希望它打印具有最高值的变量的名称。所以,在运行这个程序时,我希望它打印“Vader”。有没有办法做到这一点?

如果这似乎是一个可怕的幼稚问题,我很抱歉,但我对编程比较陌生。

谢谢, 海登

【问题讨论】:

  • 我认为c = new Console (); 不会编译。构造函数 Console() 不可见

标签: java arrays sorting console


【解决方案1】:
  • Java 没有提供以您需要的方式访问变量名称的标准方法...
  • 在创建数组时,信息会丢失...它是按值传递,而不是按引用传递,因此您将值 10、4、21 和 5 的数组传递给 Java

因此,您需要以某种方式链接值和名称...

  • 您可以关注各种枚举,并在其他问题中切换技巧,但我认为这些都没有抓住重点
  • 我建议采用更面向对象的方法,使用一个名为 Person 的类,将名称和值存储在一起...
  • Arrays.sort() 不知道如何对 Person 对象进行排序,所以它要么要求 Person 实现 java.lang.Comparable,要么传递一个可选的 java.util.Comparator,我已经包含了后者的示例。

代码

public class StackOverflowQuestion {
    private class Person {
        private String name;
        private int value;
        public String getName() {
            return name;
        }
        public int getValue() {
            return value;
        }
        public Person(String name, int value) {
            super();
            this.name = name;
            this.value = value;
        }
    }

    private class Sorting {
        public Sorting() {
            Person[] people = { new Person("obi", 10), 
                                new Person("luke", 4),
                                new Person("vader", 21), 
                                new Person("palpatine", 5) };

            Arrays.sort(people, new Comparator<Person>() {
                @Override
                public int compare(Person o1, Person o2) {
                    return o1.getValue() - o2.getValue();
                }
            });
            System.out.println(people[people.length - 1].getName());
        }
    }
    public static void main(String[] args) throws IOException {
        new StackOverflowQuestion().new Sorting();
    }
}

【讨论】:

  • 为什么PersonSorting 是静态的?
  • 我将 Sorting 设为静态,因此它可以从 main(String [] args) 实例化,而无需执行 new StackOverflowQuestion().new Sorting(),但这超出了您的问题范围,我'将更新我的答案...
【解决方案2】:

您正在尝试打印变量名称而不是值。理想情况下,您不能这样做。但是您可以尝试与enums 类似的东西。此外,c = new Console (); 将无法编译,如果您使用的是 java.io.Console,则您无法以这种方式初始化,因为 Console 构造函数是 private

例如:

public class Test {
public void Sorting() {
    int[] people = {Values.obi.getCode(), Values.luke.getCode(), Values.vader.getCode(), Values.palpatine.getCode()};
    Arrays.sort(people);
    System.out.println(Values.getEnum(people[people.length - 1]));
}

public static void main(String[] args) {
    Test s = new Test();
    s.Sorting();
}

public enum Values {
    obi(10), luke(4), vader(21), palpatine(5);

    private int code;

    Values(int i) {
        this.code = i;
    }

    public int getCode() {
        return code;
    }

    public static Values getEnum(int code) {
        switch (code) {
            case 10:
                return obi;
            case 4:
                return luke;
            case 21:
                return vader;
            case 5:
                return palpatine;
            default:
                return null;
        }
    }
  }
}

【讨论】:

    【解决方案3】:

    `公共类 StackOverflowQuestion {

    public void sorting() {
        final int obi = 10;
        final int luke = 4;
        final int vader = 21;
        final int palpatine = 5;
        int[] people = { obi, luke, vader, palpatine };
        Arrays.sort(people);
        // max value
        int max = people[people.length - 1];
        System.out.println("Max Value:" + max);
        switch (max) {
        case obi:
            System.out.println("Obi");
            break;
        case luke:
            System.out.println("Luke");
            break;
        case vader:
            System.out.println("Vader");
            break;
        case palpatine:
            System.out.println("Palpatine ");
            break;
        }
    }
    
    public static void main(String[] args) {
        StackOverflowQuestion s = new StackOverflowQuestion();
        s.sorting();
    }
    

    }`

    【讨论】:

    • 这不会很好地扩展到一百人。
    • 是的,你不能将一百个人声明为变量。我只是用你现有的代码来回答你的问题。
    【解决方案4】:

    您应该使用 String 数组而不是 int 数组。

    String[ ] people = {"obi", "luke", "vader", "palpatine"};
    Arrays.sort(people);
    System.out.println(people[people.length-1]);
    

    已编辑 在这种情况下,您应该使用此策略。

    public class Test {
    public static void main(String[] args) {        
        Person[] persons = new Person[4];
            persons[0] = new Person("obi", 10);
            persons[1] = new Person("luke", 4);
            persons[2] = new Person("vader", 21);
            persons[3] = new Person("palpatine ", 5);
        Arrays.sort(persons);
        System.out.println(persons[persons.length - 1].getName());
    }
    }
    class Person implements Comparable<Person>{
        private String name;
        private int number;
        public Person(String name, int number) {
            this.name = name;
            this.number = number;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getNumber() {
            return number;
        }
        public void setNumber(int number) {
            this.number = number;
        }
        @Override
        public int compareTo(Person otherPerson) {
            Person thisPerson = this;
            return thisPerson.getNumber() > otherPerson.getNumber() ? 1 : -1;
        }
    }
    

    【讨论】:

    • 我编辑了我的答案。 复制以上代码到Test.java文件。 @亚当
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多