交换数组列表的内容

时间:2014-12-13 19:54:37

标签: java arraylist collections swap

嗨我需要同时交换3个arraylist的内容,但我有错误。

//This is my methods
public void reorder(int a, int b){
    Collections.swap(trackNum,a,b);
    Collections.swap(name,a,b);
    Collections.swap(duration,a,b);
}

//This is me using the methods
case 3:
    meth.print();
    System.out.println("What is the first index you would like to swap:"); 
        meth.reorder(in.nextInt());
    System.out.println("What is the second index you would like to swap:");
        meth.reorder(in.nextInt());
    break;

有人可以解释我哪里出错了。

msg im获取的错误是“类型方法中的方法reorder(int,int)不适用于参数(int)”

2 个答案:

答案 0 :(得分:2)

您的方法调用与签名不匹配。你只传递一个int,你应该传递两个。

public void reorder(int a, int b){ ... } // Here you specify two arguments.
...
meth.reorder(in.nextInt()); // Here you only pass one.

//This is me using the methods
case 3:
    meth.print();
    System.out.println("What is the first index you would like to swap:"); 
    int first = in.nextInt();
    System.out.println("What is the second index you would like to swap:");
    int second = in.nextInt();
    meth.reorder(first, second);
    break;

答案 1 :(得分:0)

最明显的错误是你使用多个集合时应该有一个类型,它包含你需要的所有字段以及所有这些信息的单个集合。

  

我得到了错误,"类型方法中的方法reorder(int,int)不适用于参数(int)"

在这种情况下,您只需要重新排序一次,并且只有在您拥有所有信息时才需要。即你需要两个数字。

我建议您将第一个值保存到变量中,并且只有在获得第二个值时才调用reorder()。

System.out.println("What is the first index you would like to swap:"); 
int a = in.nextInt();
System.out.println("What is the second index you would like to swap:");
int b = in.nextInt();
meth.reorder(a, b);