2016-12-03 3 views
1

listObjectのコピーを作成するにはどうすればよいですか? listObjectカスタマイズオブジェクトのコピーを作成するには? Java

クラス:ここ

static class listObject { 
     ArrayList<String> diseases; 
     ArrayList<String> images; 

     static class guide { 
      ArrayList<String> guideTitle; 
      ArrayList<String> guideText; 
      ArrayList<String> guideImage; 
     } 
    } 

は私コピー機能である:

public static Object copy(Object orig) { 
     Object obj = null; 
     try { 
      // Write the object out to a byte array 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      ObjectOutputStream out = new ObjectOutputStream(bos); 
      out.writeObject(orig); 
      out.flush(); 
      out.close(); 

      // Make an input stream from the byte array and read 
      // a copy of the object back in. 
      ObjectInputStream in = new ObjectInputStream(
        new ByteArrayInputStream(bos.toByteArray())); 
      obj = in.readObject(); 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } 
     catch(ClassNotFoundException cnfe) { 
      cnfe.printStackTrace(); 
     } 
     return obj; 
    } 

私はコピー()関数を呼び出すと、それはリターンヌルです:

listObject list; 
listObject _tempList = new listObject(); 
list = (listObject) copy((Object) _tempList); 
+2

'obj'がnull以外の値に設定することができる唯一の場所はin.readObject''であるので、そのメソッドがnullを返しているように見えます。 –

+2

listObjectクラスは 'serializable'または' externalizable'インターフェイスを実装していますか?これらのクラスだけをストリームから読み込むことができます。 –

+0

上記の 'salud'は何ですか? – nullpointer

答えて

0

listObjectはSerializableを実装する必要があります。以下のようにlistObjectを変更します。

class listObject implements Serializable{ 
ArrayList<String> diseases; 
ArrayList<String> images; 

static class guide { 
    ArrayList<String> guideTitle; 
    ArrayList<String> guideText; 
    ArrayList<String> guideImage; 
} 
} 
関連する問題