我收到有关不兼容类型的错误:排序无法转换为java.util.arraylist<java.lang.Integer>



在我的主方法中,我收到了不兼容类型的错误消息。我创建了一个printList方法来打印输出。我相信问题与这种方法有关,但到目前为止我还搞不清楚。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Sort 
{
/*  fun printList list =
              let
                val counter = ref 0
                val temp = ref ""
                val n = length(list)
              in
                while (!counter < n) do 
                  ( temp := !temp ^ (Int.toString(List.nth(list,!counter))^" ");
                  counter := !counter+1);
                temp := !temp ^ "n";
                !temp 
              end;
*/
    public static String printList (ArrayList<Integer> left)
    {
        int counter = 0;
        String temp = "";
        int n = left.size();
        while(counter < n)
        {
            temp += left.get(counter)+ " ";
            counter += 1;
        }
        temp += "n";
        return temp;
    }
/*
            fun lineList file =
              let
                val instr = TextIO.openIn file
                val str   = TextIO.input instr
              in 
                String.tokens (fn x => x = #",")str
              before
                TextIO.closeIn instr
              end;
*/
    public ArrayList<Integer> lineList(String file) throws FileNotFoundException
    {
        ArrayList<Integer> tmp = new ArrayList();
        try {
            Scanner scanner = new Scanner(new File(file));
            scanner.useDelimiter(",");
            int i = 0;
            while (scanner.hasNextInt()) {
                tmp.add(scanner.nextInt());
                i++;
            }
            scanner.close();
            } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return tmp;
    }
/*
            fun halve nil = (nil, nil)  
            |   halve [a] = ([a], nil)
            |   halve (a :: b :: cs) =
                  let
                    val (x, y) = halve cs
                  in
                    (a :: x, b :: y)
                  end;
*/
    public static ArrayList<Integer> halve(ArrayList<Integer> l, String subL){
        int halveSize = l.size() / 2;
        ArrayList<Integer> halfList = new ArrayList<Integer>(halveSize);
        if(subL == "left"){
            for(int i = 0; i < l.size(); i += 2){
                halfList.add(l.get(i));
            }
        }else if(subL == "right"){
            for(int i = 1; i < l.size(); i += 2){
                halfList.add(l.get(i));
            }
        }
        return halfList;
    }
/*
            fun merge (nil, ys) = ys
            |   merge (xs, nil) = xs
            |   merge (x :: xs, y :: ys) =
                  if (x > y) then x :: merge(xs, y :: ys)
                  else y :: merge(x :: xs, ys);
*/
     public static ArrayList<Integer> merge(ArrayList<Integer> le, ArrayList<Integer> r) {
         ArrayList<Integer> result = new ArrayList<Integer>();
         int i = 0;
         int j = 0;
         while (i < le.size() && j < r.size()) {
             if (le.get(i) <= r.get(j)) {
                 result.add(le.get(i));
                 i++;
             }else {
                 result.add(r.get(j));
                 j++;
             }
         }
         for (int k=0;k<le.size();k++) {
             if (k>i) {
                 result.add(le.get(k));
             }
         }
         for (int l=0;l<r.size();l++) {
             if (l>j) {
                 result.add(r.get(l));
             }
         }
         return result;
     }


/*
            fun mergeSort nil = nil
            |   mergeSort [a] = [a]
            |   mergeSort theList =
                  let
                    val (x, y) = halve theList
                  in
                    print("xList: "^printList(x));
                    print("yList: "^printList(y));
                    merge(mergeSort x, mergeSort y)
                  end;
*/
     public static ArrayList<Integer> mergeSort(ArrayList<Integer> l){
        if(l.size() <= 1){
            return l;
        }
        ArrayList<Integer> xList = halve(l, "left");
        ArrayList<Integer> yList = halve(l, "right");
        System.out.print("xList: " +printList(xList));
        System.out.print("yList: " +printList(yList));
        return merge(mergeSort(xList), mergeSort(yList));
    }
/*
            fun quicksort nil = nil
            |   quicksort (pivot :: rest) =
                  let
                    fun split(nil) = (nil,nil)
                    |   split(x :: xs) =
                          let
                            val (below, above) = split(xs)
                          in
                            if x < pivot then (x :: below, above) 
                            else (below, x :: above)
                          end;
                    val (below, above) = split(rest)
                  in
                    quicksort below @ [pivot] @ quicksort above
                  end;
            val optList = map (fn str => (Int.fromString str)) (lineList "/home/chella/Documents/SMLCode1/input.txt");
            val intList = map Option.valOf optList;
            fun uselessMachine x = mergeSort(quicksort(x));
            uselessMachine(intList);
*/
    public static ArrayList<Integer> quickSort(ArrayList<Integer> input)
    {
        if(input.size() <= 1){
            return input;
        }
        int centre = input.get(0);
        ArrayList<Integer> a = new ArrayList<Integer>();
        ArrayList<Integer> b = new ArrayList<Integer>();
        for(int i = 1; i < input.size(); i++){
            if(input.get(i) < centre){
                a.add(input.get(i));
            }else{
                b.add(input.get(i));
            }
        }
        ArrayList<Integer> output = quickSort(a);
        output.add(centre);
        output.addAll(quickSort(b));
        return output;
    }
    public static void main(String[] args)
    {
        //Sort <Integer>s= new Sort<Integer>();
        Sort s= new Sort ();
        //int [] a = {1,2,3,4,8,10};
        //System.out.println(s.printList(a));
        try {
            s.mergeSort(s.quickSort(s.lineList("input.txt")));
        System.out.println(printList(s)); 
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        }
    }
}

错误消息非常清楚。。。

排序无法转换为java.util.arraylist

问题出在这一行:

System.out.println(printList(s));

根据该初始化,sSort的实例

Sort s = new Sort();

printList的签名是

public static String printList (ArrayList<Integer> left)

假设其接收ArrayList<Integer>而不是Sort对象。

我想你想用的是

System.out.println(printList(s.lineList("input.txt"))); 

这将修复错误。

编辑:以下是我在评论中谈到的片段:

Sort s = new Sort();
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
a.add(4);
a.add(8);
a.add(10);
System.out.println(printList(a));

此外,如果你想按reverse的顺序打印,那么你必须使用printList方法中的Collections包反转list,现在看起来像这样:

public static String printList (ArrayList<Integer> left)
{
    int counter = 0;
    String temp = "";
    int n = left.size();
    Collections.reverse(left);
    while(counter < n)
    {
        temp += left.get(counter)+ " ";
        counter += 1;
    }
    temp += "n";
    return temp;
}

则输出将是10 8 4 3 2 1

最新更新