arraylist给“不能解决”和“不能解决一个类型”的错误(日食)
问题描述:
我正在写一个选择排序程序,它仍然是未完成的,所以有很多错误,但我'我想找首先要解决的是,日食错误不断给我:arraylist给“不能解决”和“不能解决一个类型”的错误(日食)
thelist cannot be resolved to a type
,并再次,在程序的其余部分:
thelist cannot be resolved
我的程序是这样的:
import java.util.ArrayList;
public class SelectionSort {
public static void main(String[] args) {
final int N = Integer.parseInt(args[0]);
//populate list with number from 1 to N
// Create an ArrayList containing Integer type elements
ArrayList<Integer> thelist = new ArrayList();
//while loop to accept a new value from random generator
//call sort method
}
public static void sort(final thelist<Integer> list) {
// declare an int variable to hold value of index at which the element
// has the smallest value
int smaller = 0;
int b=0;
// declare an int variable to hold the smallest value for each iteration
// of the outer loop
int smallerindex = 0;
for(int a=1;a<thelist.size();a++){
/* find the index at which the element has smallest value */
// initialize variables
smaller = thelist.get(a-1);
smallerindex = a-1;
for(b=a;b<thelist.size();b++){
if(thelist.get(b)<smaller){
// update smallest
smaller = thelist.get(b);
smallerindex = b;
}
}
// do nothing if the curIndex has the smallest value
//Swap the smallest element with the first element of unsorted subarray
int temp = thelist.get(destIndex);
thelist.set(destIndex, thelist.get(sourceIndex));
thelist.set(sourceIndex, temp);
}
}
}
答
在你的排序方法你是给的thelist作为参数列表的类型:
public static void sort(final thelist<Integer> list) {
改变它:
public static void sort(final ArrayList<Integer> list) {
然后在main方法当你需要调用排序方法时,你可以这样称呼它:
//call sort method
sort(thelist);
这就是您将列表“列表”并将其用于您的方法的方式。
+0
您还需要将方法体中的'listlist'更改为'list',或者将参数名称更改为'thelist'。 – 2015-03-25 00:48:26
'thelist'不是一个有效的类型。排序方法的签名应该是'sort(ArrayList list)'。 –
2015-03-24 22:58:39
谢谢你,工作! – 2015-03-24 22:59:50