-4

4つのデータメンバー、2つの文字列、1つの整数、および別のクラスのオブジェクトのリストを持つクラスのオブジェクトを格納するリンクリストがあります。

リンクされたリスト内のリストの値にアクセスするにはどうすればよいですか?

わかりました、これは私が持っているものです

LogProcess lp=新しい LogProcess(リビジョン、作成者、日付、パス情報、msg);

pathinfo は List.revision,author,date,msg の形式のリストです。文字列です。

PathInfo は、action、kind、pathinfo の 3 つのデータ メンバーを持つクラスです。3 つすべてが文字列です。

リンクされたリストをコンストラクターに渡し、action、kind、および pathinfo の値にアクセスしたい場合、どうすればよいでしょうか?

4

5 に答える 5

1

を使用していると仮定すると、LinkedListを呼び出しますget。ジェネリックを使用していない場合 (LinkedList<YourFooObject> YourLinkedList;使用している場合、使用しLinkedList YourLinkedListない場合)、次のようにキャストが必要になります。

 YourFooObject foo = (YourFooObject)YourLinkedList.get(0)
于 2012-08-05T22:32:09.457 に答える
0

私の意見では、これを実行するための最良の方法は、次のようなオブジェクト配列/リスト/コレクションのforeachループを使用するようなものです。

for(Object o : list) {
     if(o instanceof String) {
        //Object in list is a string
        String s = (String)o; //make sure to properly cast the object now that you know it is of proper type
     }
     else if(o instanceof int) {
        //object in list is an int
     }
     else if(o instanceof classA) {
        //object in list is from classA and you get the idea
     }
     else if(o instanceof classB) {
     }
//etc..
}
于 2012-08-05T22:40:42.723 に答える
0

あなたの特定LinkedListのあなたのオブジェクトにアクセスするためのイテレータを作成できます。次のようなものが機能します。LinkedListclass object

LinkedList<YourObject> list = new LinkedList<YourObject>();
ListIterator listIt = list.listIterator();
YourObject sample = listIt.next();//or any of the objects you need in your list
int valueOfSample = sample.getFirstParameter();//Assuming getFirstParameter is a method of your object class that returns the first integer parameter
于 2016-12-07T10:14:16.203 に答える
0
//Retriver one of the nodes of the linked list
YourObjectType node = yourLinkedList.get(int index)

//access the list instance variable of your object (depending on it visibility)
node.myList; //if accessible from here
MyList myList = node.getMyList();
于 2012-08-05T22:33:18.787 に答える
0

get(index)LinkedListの関数は、目的のインデックスにある要素を返します。

YourStructure data = list.get(someindex);

これで、単一のオブジェクトからリストを取得できます。

LinkedList<Something> sublist = data.getList();

次に、通常のようにそのサブリストを反復処理できます:forループ、 a foreach、またはiterator.

于 2012-08-05T22:36:31.290 に答える