Java 如何根据一个数组对两个数组进行排序';s值
按字母顺序正确显示时间和CHAMP的位置 但是当我使用Java 如何根据一个数组对两个数组进行排序';s值,java,Java,按字母顺序正确显示时间和CHAMP的位置 但是当我使用Arrays.sort(champs)时 我的输出是: Time----Champs 2001 Banana 2004 Banana 2000 Boat 2003 Boat 2011 Carrot 2013 Carrot 2002 Cucumber champs的产量显示正确,但年份按1递减 如果没有数组,sort(champs)我的输出是: Time----Champs 2004 Banana 2005 Ban
Arrays.sort(champs)时代码>
我的输出是:
Time----Champs
2001 Banana
2004 Banana
2000 Boat
2003 Boat
2011 Carrot
2013 Carrot
2002 Cucumber
champs的产量显示正确,但年份按1递减
如果没有数组,sort(champs)
我的输出是:
Time----Champs
2004 Banana
2005 Banana
2006 Boat
2007 Boat
2008 Carrot
2009 Carrot
2010 Cucumber
正如你所看到的,冠军的时间是正确的,但不是按字母顺序排列的 你想看看这里吗
这里是冠军
值-->时间
我认为你想缩短你的冠军和时间应该保持相应的价值观。
希望对您有所帮助。您想看看这里吗
这里是冠军
值-->时间
我认为你想缩短你的冠军和时间应该保持相应的价值观。
希望对您有所帮助。我认为您的问题是您没有与“冠军”一起重新安排“时间”。
从你的例子来看,似乎“时间”只是逐年递增的年份,而冠军就是当年的冠军队伍。
当你把冠军按字母顺序排序时,他们与时间不同步
要解决这个问题,您需要将时间与Champs配对,这样,如果您按其中一个值排序,另一个值也会随之移动
从这样的内部类开始:
Time----Champs
2000 Boat
2001 Banana
2003 Boat
2004 Banana
2002 Cucumber
2011 Carrot
2013 Carrot
然后你可以把结果打印出来
Arrays.sort(timechamps);
我认为你的问题是你没有和“冠军”一起点“时间”。
从你的例子来看,似乎“时间”只是逐年递增的年份,而冠军就是当年的冠军队伍。
当你把冠军按字母顺序排序时,他们与时间不同步
要解决这个问题,您需要将时间与Champs配对,这样,如果您按其中一个值排序,另一个值也会随之移动
从这样的内部类开始:
Time----Champs
2000 Boat
2001 Banana
2003 Boat
2004 Banana
2002 Cucumber
2011 Carrot
2013 Carrot
然后你可以把结果打印出来
Arrays.sort(timechamps);
最好创建一个类并实现类似的方法
for (Tuple t : timechamps) {
System.out.println(t.time+"\t"+t.champ);
}
}
公共类TimeChamp实现了可比较的{
私人短时间;
私人弦乐冠军;
公共短getTime(){
返回时间;
}
公共无效设置时间(短时间){
这个时间=时间;
}
公共字符串getChamps(){
返回冠军;
}
公共无效设置CHAMP(字符串CHAMP){
this.champs=champs;
}
@凌驾
公共国际比较(TimeChamp o){
if(getChamps().compareTo(o.getChamps())==0)
{
如果(getTime(),最好创建一个类并实现类似的方法
for (Tuple t : timechamps) {
System.out.println(t.time+"\t"+t.champ);
}
}
公共类TimeChamp实现了可比较的{
私人短时间;
私人弦乐冠军;
公共短getTime(){
返回时间;
}
公共无效设置时间(短时间){
这个时间=时间;
}
公共字符串getChamps(){
返回冠军;
}
公共无效设置CHAMP(字符串CHAMP){
this.champs=champs;
}
@凌驾
公共国际比较(TimeChamp o){
if(getChamps().compareTo(o.getChamps())==0)
{
if(getTime()我也试过了。这是一个功能齐全、自包含的解决方案
import java.util.Arrays;
public class MainExample {
/**
* @param args
*/
public static void main(String[] args) {
//Construct data sets
TimeChamp [] timeChampArray = new TimeChamp[3];
TimeChamp timeChamp = new TimeChamp();
timeChamp.setChamps("Boat");
timeChamp.setTime((short)2001);
timeChampArray[0]=timeChamp;
timeChamp= new TimeChamp();
timeChamp.setChamps("Banana");
timeChamp.setTime((short)2000);
timeChampArray[1]=timeChamp;
timeChamp= new TimeChamp();
timeChamp.setChamps("Banana");
timeChamp.setTime((short)2001);
timeChampArray[2]=timeChamp;
//Call the sort method
new MainExample().getSort(timeChampArray);
}
//the sorting method
public void getSort(TimeChamp[] timeChamp)
{
Arrays.sort(timeChamp);
for(int i=0;i<timeChamp.length;i++)
{
System.out.print(timeChamp[i].getTime());
System.out.print(" ");
System.out.println(timeChamp[i].getChamps());
}
}
导入java.util.array;
公共类时间和排序器{
公共静态void main(字符串[]args){
//准备输入
短[]时间={2000、2001、2003、2004、2002、2011、2013};
字符串[]champs={“船”、“香蕉”、“船”、“香蕉”、“黄瓜”,
“胡萝卜”、“胡萝卜”};
印刷品分类(时间,冠军);
}
公共静态无效打印排序(短[]时间,字符串[]个字符){
//检查参数的长度相等
if(time.length!=champs.length){
抛出新的IllegalArgumentException(
“数组时间和CHAMP长度必须相等”);
}
Tuple[]tuples=createTuples(时间,champs);
排序(元组);
打印元组(tuples);
}
私有静态元组[]createTuples(短[]时间,字符串[]个){
//创建长度正确的元组空数组
Tuple[]tuples=新的Tuple[champs.length];
//填充元组数组
for(int i=0;i
我也试过了。这是一个功能齐全的独立解决方案
import java.util.Arrays;
public class MainExample {
/**
* @param args
*/
public static void main(String[] args) {
//Construct data sets
TimeChamp [] timeChampArray = new TimeChamp[3];
TimeChamp timeChamp = new TimeChamp();
timeChamp.setChamps("Boat");
timeChamp.setTime((short)2001);
timeChampArray[0]=timeChamp;
timeChamp= new TimeChamp();
timeChamp.setChamps("Banana");
timeChamp.setTime((short)2000);
timeChampArray[1]=timeChamp;
timeChamp= new TimeChamp();
timeChamp.setChamps("Banana");
timeChamp.setTime((short)2001);
timeChampArray[2]=timeChamp;
//Call the sort method
new MainExample().getSort(timeChampArray);
}
//the sorting method
public void getSort(TimeChamp[] timeChamp)
{
Arrays.sort(timeChamp);
for(int i=0;i<timeChamp.length;i++)
{
System.out.print(timeChamp[i].getTime());
System.out.print(" ");
System.out.println(timeChamp[i].getChamps());
}
}
导入java.util.array;
公共类时间和排序器{
公共静态void main(字符串[]args){
//准备输入
短[]时间={2000、2001、2003、2004、2002、2011、2013};
字符串[]champs={“船”、“香蕉”、“船”、“香蕉”、“黄瓜”,
“胡萝卜”、“胡萝卜”};
印刷品分类(时间,冠军);
}
公共静态无效打印排序(短[]时间,字符串[]个字符){
//检查参数的长度相等
if(time.length!=champs.length){
抛出新的IllegalArgumentException(
“数组时间和CHAMP长度必须相等”);
}
Tuple[]tuples=createTuples(时间,champs);
import java.util.Arrays;
public class MainExample {
/**
* @param args
*/
public static void main(String[] args) {
//Construct data sets
TimeChamp [] timeChampArray = new TimeChamp[3];
TimeChamp timeChamp = new TimeChamp();
timeChamp.setChamps("Boat");
timeChamp.setTime((short)2001);
timeChampArray[0]=timeChamp;
timeChamp= new TimeChamp();
timeChamp.setChamps("Banana");
timeChamp.setTime((short)2000);
timeChampArray[1]=timeChamp;
timeChamp= new TimeChamp();
timeChamp.setChamps("Banana");
timeChamp.setTime((short)2001);
timeChampArray[2]=timeChamp;
//Call the sort method
new MainExample().getSort(timeChampArray);
}
//the sorting method
public void getSort(TimeChamp[] timeChamp)
{
Arrays.sort(timeChamp);
for(int i=0;i<timeChamp.length;i++)
{
System.out.print(timeChamp[i].getTime());
System.out.print(" ");
System.out.println(timeChamp[i].getChamps());
}
}
import java.util.Arrays;
public class TimeAndChampSorter {
public static void main(String[] args) {
// prepare input
short[] time = { 2000, 2001, 2003, 2004, 2002, 2011, 2013 };
String[] champs = { "Boat", "Banana", "Boat", "Banana", "Cucumber",
"Carrot", "Carrot" };
printSorted(time, champs);
}
public static void printSorted(short[] time, String[] champs) {
// check arguments are of equal length
if (time.length != champs.length) {
throw new IllegalArgumentException(
"arrays time and champs must have equal length");
}
Tuple[] tuples = createTuples(time, champs);
Arrays.sort(tuples);
printTuples(tuples);
}
private static Tuple[] createTuples(short[] time, String[] champs) {
// create empty array of Tuples with correct length
Tuple[] tuples = new Tuple[champs.length];
// fill the tuples array
for (int i = 0; i < champs.length; i++) {
tuples[i] = new Tuple(time[i], champs[i]);
}
return tuples;
}
private static void printTuples(Tuple[] tuples) {
System.out.println("Time Champs\n");
for (Tuple tuple : tuples) {
System.out.println(tuple);
}
}
// static class to avoid having to create an instance of TimeAndChampSorter
static class Tuple implements Comparable<Tuple> {
short time;
String champ;
Tuple(short time, String champ) {
// make sure champ is not null to avoid having to test for nulls in
// compareTo
if (champ == null) {
throw new IllegalArgumentException("champ can not be null");
}
this.time = time;
this.champ = champ;
}
// method of Comparable interface determines the ordering
@Override
public int compareTo(Tuple other) {
return this.champ.compareTo(other.champ);
}
@Override
public String toString() {
return time + " " + champ;
}
}
}