-4

我有一个链表,它存储一个类的对象,它有 4 个数据成员、2 个字符串、一个整数和另一个类的对象列表。

如何访问链接列表中列表的值?

好的,这就是我所拥有的

LogProcess lp=new LogProcess(revision,author,date,pathinfo,msg);

pathinfo 是 List.revision 格式的列表,作者、日期、msg 是字符串。

现在 PathInfo 是一个具有三个数据成员 action、kind、pathinfo 的类。所有 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
//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

LinkedList的get(index)函数将返回所需索引处的元素。

YourStructure data = list.get(someindex);

现在您可以从单个对象中获取列表:

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

然后您可以像往常一样遍历该子列表:使用for循环、aforeach或 an iterator

于 2012-08-05T22:36:31.290 回答
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来访问LinkedList您的特定对象class 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 回答