arraylist给出“无法解决”和“无法解决的类型”错误(eclipse)

时间:2015-03-24 22:51:53

标签: java eclipse arraylist selection-sort

我正在编写一个选择排序程序,它仍然未完成,所以有很多错误,但我想先修复的那个是eclipse一直给我的错误:

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);

        }
    }

}

1 个答案:

答案 0 :(得分:1)

在您的排序方法中,您将该列表作为参数列表的类型:

public static void sort(final thelist<Integer> list) {

将其更改为:

public static void sort(final ArrayList<Integer> list) {

然后在main方法中,当你需要调用sort方法时,你可以这样调用它:

//call sort method
sort(thelist);

这就是你的名单和#34; thelist&#34;并在你的方法中使用它。