在链表的正确位置输入对象
我有一个包含几行的 csv 文件(组号、组中的元素号、元素号),我需要将它们放置在链接列表中。我在文件读取 csv 时发生这种情况,将其放入 tmpPacket 对象中,然后将 tmpPackets 放入 nodeList(链接列表)中,并尝试将其按顺序添加到链接列表中,以便如果组# 与前一个相同,它将它添加到该组的开头,否则添加到链表的末尾。
不管怎样,到目前为止,我已经将它添加到了链接列表中,但忽略了其余的组。示例输入是:
4,3,2
5,1,1
4,3,1
4,3,3
2,2,2
3,1,1
2,2,1
基本上我想要它,所以当它被添加到链接列表时,它看起来像:(
4,3,1
4,3,2
4,3,3
5,1,1
2,2,1
2,2,2
3,1,1
确切的顺序并不重要。4、5、2和3可以是任何顺序,重要的是4 在一起,5 在一起......)。
这是我所拥有的,仅输出 4,没有其他内容。
int currLength = nodeList.getLength();
int finishNum = 0;
for(int tmpGo=1;tmpGo<=currLength;tmpGo++){
if(finishNum == 0){
int itr = 0;
int addEnd = 0;
while(itr<nodeList.getLength()){
itr++;
if(nodeList.getEntry(itr).getPageID() == pageID) {
nodeList.add(tmpGo, tmpPacket);
finishNum = 1;
addEnd = 1;
break;
}
}
} else {
break;
}
}
I have a csv file with a few rows (group #, # of elements in group, element #) and I need to place these inside of a Linked List. I have this happening while the csv is being read in by the file, putting it into the tmpPacket object, then placing the tmpPackets into the nodeList (linked list) and am trying to have it adding to the Linked List in order so if the group # is the same as a previous one, it adds it to the beginning of that group, otherwise to the end of the linked list.
Anyways, I have it so far working to the point where it will add one group # to the Linked List, but ignores the rest of the groups. example input would be:
4,3,2
5,1,1
4,3,1
4,3,3
2,2,2
3,1,1
2,2,1
and basically I want it so when it is added to the linked list it will look like:
4,3,1
4,3,2
4,3,3
5,1,1
2,2,1
2,2,2
3,1,1
(the exact order doesn't matter. 4, 5, 2, and 3 can be in any order, important is that the 4's are together, 5's are together...).
Here is what I have that is only outputting the 4's and nothing else.
int currLength = nodeList.getLength();
int finishNum = 0;
for(int tmpGo=1;tmpGo<=currLength;tmpGo++){
if(finishNum == 0){
int itr = 0;
int addEnd = 0;
while(itr<nodeList.getLength()){
itr++;
if(nodeList.getEntry(itr).getPageID() == pageID) {
nodeList.add(tmpGo, tmpPacket);
finishNum = 1;
addEnd = 1;
break;
}
}
} else {
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,我不知道你的nodeList是什么,但根据你最初的描述,你需要这个:
这个例子假设:
但是,它不会产生您的样本结果。相反,你会得到:
So, I don't know what your nodeList is, but according to your initial description you would need this:
This example assumes:
However, it will not result in your sample result. Instead you will get: