0

私は Java の初心者で、バイナリ ファイルの入力と配列リストの出力に問題があります。配列リストのデータをファイルに保存し、それを使用してコンソールに表示しようとしています。これが私のコードの一部です。実行されますが、間違った情報が表示され、警告も表示されます。この問題の原因について何か助けはありますか? ありがとう!

public class Towers {
    public static ArrayList<String> allMoves= new ArrayList<String>();
    static{
        allMoves.add( "These Are the Disk Moves:" );
    }

    public static void move(final int aNumDisks){
        move(aNumDisks, 1, 2, 3);
        String fileName = "solution.dat";
        try{
            ObjectOutputStream outputStream = 
                    new ObjectOutputStream(
                            new FileOutputStream (fileName));
            outputStream.writeObject(allMoves);
            outputStream.close( );
        }
        catch ( IOException e ){
            System.out.println("Error writing to file " + fileName);
            System.exit(0);
        }
    }

出力ファイルをコンソールに表示します。

public class TReporter {        
    public static void reportSol(){
        String fileName = "solution.dat";
        ArrayList<String> allMovesA = null;
        try{
            ObjectInputStream inputStream =
                    new ObjectInputStream(
                            new FileInputStream (fileName));
            allMovesA = (ArrayList<String>)inputStream.readObject(); //WARNING!
                           //Type safety: Unchecked cast from Object to ArrayList<String>
            inputStream.close();
        }
        catch (Exception e){
            System.out.println("Problem reading the file " + fileName);
            System.exit(0);
        }
        for (int i = 0; i < allMovesA.size(); ++i){
            System.out.println( allMovesA.get(i) );
        }

次のように表示されます。

=== 0 disks move(int) ===
These Are the Disk Moves:

=== 0 disks move(,,) ===
These Are the Disk Moves:

=== 1 disk move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3

=== 2 disks ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 2 disks move(2,3,2,1) ===
These Are the Disk Moves:
Move disk from 3 to 2
Move disk from 3 to 1
Move disk from 2 to 1

=== 3 disks move(3) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

しかし、私はこれを得ています:

=== 0 disks move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

=== 0 disks move(,,) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

=== 1 disk move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3

=== 2 disks ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 2 disks move(2,3,2,1) ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 3 disks move(3) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3
4

1 に答える 1

2

表示される警告は完全に正常です。警告のある行の前に次の行を挿入することで抑制できます。

@SuppressWarnings("unchecked")

そして、あなたが得た間違った情報について:

  • 古いバージョンのファイルを使用している可能性があります。
  • ArrayList に正しい動きが含まれていることを確認してください。
于 2012-04-24T16:52:54.670 に答える