问题描述
我使用.也就是说,我已经完成:
I've created an array of LinkedList of Connection
objects using the second answer from here. That is, I've done:
LinkedList< Connection> []映射=(LinkedList< Connection> [])new LinkedList [count];
但是,我对如何访问数组的每个元素(即每个 LinkedList)并创建一个新节点感到困惑.现在,我有:
However, I am confused as to how to access each element of the array (ie each LinkedList) and create a new node. Right now, I have:
for (int j = 0; j < numOfConnections; j++) {
map[j].add(new Connection(s.next(), s.nextDouble(), s.next()));
}
但是我认为这只会向数组的每个LinkedList元素添加一个单个新节点.我想循环遍历并将 numOfConnections
个节点添加到每个 LinkedList元素.例如, map [0]
中的3个节点, map [1]
中的5个节点, map [2]
中的2个节点,等等.
But I think this would only add a single new node to each LinkedList element of the Array. I want to loop through and add numOfConnections
amount of nodes to each LinkedList element. For example, 3 nodes in map[0]
, 5 nodes in map[1]
, 2 nodes in map[2]
, etc.
推荐答案
在您的示例中,如果 numOfConnections,则"map [0]中有3个节点,map [1]中有5个节点,map [2]中有2个节点"
是您要映射到要添加的列表的要添加到LinkedList [k]的值的数量吗?例如:numOfConnections [] = {3,5,2};
On your example "3 nodes in map[0], 5 nodes in map[1], 2 nodes in map[2]" if numOfConnections
is the amount of values you want to add to your LinkedList[k] shoudnt you map which list to add ? eg.: numOfConnections[] = {3, 5, 2};
for ( int k = 0; k < numOfConnections.length; k++ )
{
if (map[k] == null) map[k] = new LinkedList<Connection>();
for (int j = 0; j < numOfConnections[k]; j++)
{
map[k].add(new Connection(s.next(), s.nextDouble(), s.next()));
}
}
这篇关于如何访问链表数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!