2

ArrayList<Clause>呼ばれるものがありlisttableます。何らかの理由Clause[] whatever = listtable.toArray()で互換性のない型のエラーが発生しますが、問題なくClause[] whatever = listtable.toArray(new Clause[0])動作します。これはなぜですか?2 つの呼び出しの違いは何ですか? javadoc は、「機能が同一」であると述べています。

これが私の完全なコードです(関連するステートメントは最後の直前にあります):

public static Clause[] readCNF(String name,Boolean print) throws IOException 
    {
        BufferedReader file = new BufferedReader(new FileReader("./" + name));

        ArrayList<Clause> listtable = new ArrayList<Clause>();
        String line = null; 
        while ((line = file.readLine()) != null) {

            if(line.charAt(0) == 'p')
            {
                 Scanner scanner = new Scanner(line); 
                 scanner.next(); scanner.next(); Clause.NumVars = scanner.nextInt(); Clause.NumClauses = scanner.nextInt(); 

            } else if(line.charAt(0) != 'c') {  

                 ArrayList<Integer> lits = new ArrayList<Integer>();
                 Scanner scanner = new Scanner(line);

                 while(scanner.hasNext())
                 {
                     int var = scanner.nextInt();
                     if(var != 0){ lits.add(var);}
                 }

                 listtable.add(new Clause(lits));

            } 
        }

        if(print) {
            for(Clause clause : listtable)
            {
                clause.print();
            }
        }

       return(listtable.toArray(new Clause[0])); //since the return type is Clause[] this is the same as the statements in the question
    }
4

1 に答える 1