**这是我在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”。反正有没有这样做?
如果这看起来像一个可怕的少年问题,我很抱歉,但我对编程比较陌生。
谢谢, 海登
答案 0 :(得分:1)
因此,您需要将值和名称链接起来......
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();
}
}
答案 1 :(得分:0)
您正在尝试打印变量名而不是值。理想情况下,你不能这样做。但您可以尝试使用与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;
}
}
}
}
答案 2 :(得分:-1)
`public class 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();
}
}`
答案 3 :(得分:-2)
您应该使用String数组而不是int数组。
String[ ] people = {"obi", "luke", "vader", "palpatine"};
Arrays.sort(people);
System.out.println(people[people.length-1]);
EDITED 的 在这种情况下,您应该使用此策略。
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;
}
}