简体   繁体   English

这是否返回一个空列表?

[英]Is this returning an empty list?

I have this method that is removing elements from a list, 我有一种从列表中删除元素的方法,

List<LinkedList<Object>> list = new LinkedList<>();

that removes two elements, returns a list which is then put through a queue names=queue.removePair(); 删除两个元素,返回一个列表,然后将其放入队列中。names = queue.removePair();

public List removePair() {
    List<E> nList = new LinkedList<>();
    if (list.isEmpty() == true) {
        throw new InsufficientElementsException();
    }
    if (list.isEmpty() == false) {
        Object temp;
        for (int i = 0; i < list.size(); i++) {
            temp = list.get(i).size();
            if (temp.equals(2)) {
                nList.add((E) list.get(i));
                list.remove(i);
            }
        }
        return nList;
    }

    return nList;
}

I believe this doesn't work because when I return the Nlist, it's only returning the one declared in the first line? 我相信这是行不通的,因为当我返回Nlist时,它只返回第一行中声明的那个吗? How do I fix this? 我该如何解决?

You're using indices to remove and get elements from the list at the same time. 您正在使用索引同时从列表中removeget元素。 After a remove , your index will be wrong. remove ,您的索引将是错误的。 Use an Iterator in the for-loop instead: 在for循环中使用Iterator代替:

for (Iterator<List<Object>> iterator = list.iterator(); iterator.hasNext();) {
    List<Object> l = iterator.next();
    if (l.size() == 2) {
        nList.add((E) l);
        iterator.remove();
    }
}

Your following code: 您的以下代码:

if (temp.equals(2))

always returns false. 始终返回false。 That is why you are always getting empty list. 这就是为什么您总是得到空清单的原因。

I have written code which might solve your problem. 我已经编写了可以解决您问题的代码。 Try it out.Changes are mentioned in the comments. 尝试一下。注释中提到了更改。

// Used a generic class 
public class TestClass<T> where T:LinkedList<Object>
{
    public List<T> list = new List<T>();

    public List<T> TestMethod()
    {
        List<T> nList = new List<T>();
        // Removed IsEmpty , used Count instead
                    if (list.Count == 0) {
                        //throw new InsufficientElementsException();
                    }
                        if (list.Count > 0) {
                            int temp;
                            for (int i = 0; i < list.Count; i++)
                            {
                                // To find out the count of nodes in a particular item of the list
                                temp = list.ElementAt(i).Count;

                                T obj = list.ElementAt(i);
                                if (temp == 2)
                                {
                                    nList.Add(obj);
                                    list.RemoveAt(i);
                                }
                            }
                            return nList;
                        }

        return nList;

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM