为什么我的循环没有检索到最后的项目?

时间:2018-05-05 14:04:00

标签: java arrays loops object arraylist

我正在尝试实现一种方法来查看项目列表及其数量,而不会重复。我使用ArrayList执行此操作,该ListList将保存我创建的Item类型的对象。问题是在循环中我从原始列表的副本中删除重复项,因为它没有显示列表中的最后两个项目,我不知道如何解决它。这是代码。 Item是非常简单的对象,它包含(int identifier,int price,String name)

private ArrayList list;

public void print(ListOfItems storeList)
{
    list = storeList.getList();
    if ( list.size() == 0)
        System.out.println("Sorry! There are no available Items at the store at this moment.");
    /** I changed this section
    else
    {
        Object[] originalItems = list.toArray();
        ArrayList copy = storeList.getCopy(storeList.getList());
        Object[] copyItems = copy.toArray();
        System.out.println("Here is a list Of available items in this Store");
        System.out.println("Name\tIdentifier\tprice\tQuantity");

       //this loop is wrong
        for (int i = 0; i < originalItems.length-1; i++)
        {
            for (int j = i+1; j < originalItems.length; j++)
            {
                if (originalItems[i].equals(originalItems[j]) && copyItems[j] != null)
                {
                    copy.remove(originalItems[j]);
                }
            }
        }
        **/
        //Below is the write loop
        else
        {
        Object[] originalItems = list.toArray();
        ArrayList copy = new ArrayList(list.size());
        for (int i = 0; i < originalItems.length; i++)
        {
            Item item = (Item) originalItems[i];
            if (copy.contains(item) == false)
            {
                copy.add(item);
            }
        }

        Object[]cop = copy.toArray();
        for (int i = 0; i < cop.length; i++)
        {
            if (cop[i] != null)
            {
                Item item = (Item) copyItems[i];
                System.out.print(item.getName() + "\t");
                System.out.print(item.getIdentifier() + "\t\t");
                System.out.print(item.getPrice() + "\t");
                System.out.print(Methods.getOccurences(list, item));
                System.out.println();
            }
        }

        System.out.print("*****************************");
    }
}

这是ListOfItems的类

import java.util.ArrayList;
public class ListOfItems 
{
int numOfItemsInStore = 50;
private ArrayList list = new ArrayList(numOfItemsInStore);
public ListOfItems()
{
    Item item1 = new Item (111, 50, "Item1");
    list.add(item1);
    Item item2 = new Item (222, 99, "Item2");
    list.add(item2);
    Item item3 = new Item (333, 20, "Item3");
    list.add(item3);
    Item item4 = new Item (444, 199, "Item4");
    list.add(item4);
    Item item5 = new Item (555, 14, "Item5");
    list.add(item5);
    Item item6 = new Item (666, 40, "Item6");
    list.add(item6);
    list.add(item6);
    list.add(item6);
    list.add(item2);
    list.add(item3);
    list.add(item3);
    list.add(item3);
}

public ArrayList getList()
{
    return list;
}


public ArrayList getCopy(ArrayList listToCopy)
{
    ArrayList copy = new ArrayList(numOfItemsInStore);
    if (listToCopy.isEmpty())
        System.out.println("This list is Empty");
    else
    {

        Object[] listArray = listToCopy.toArray();
        for (int i = 0; i < listArray.length; i++)
        {
            Item item = (Item) listArray[i];
            copy.add(item);
        }
    }

    return copy;
}
}

这是Item类

public class Item 
{
private int identifier;
private int price;
private String name;

public Item (int id, int price , String name)
{
    this.identifier = id;
    this.name = name;
    this.price = price;
}

public int getIdentifier()
{
    return identifier;
}

public int getPrice()
{
    return price;
}

public String getName()
{
    return name;
}
}

3 个答案:

答案 0 :(得分:1)

好的,首先我建议使用Set来删除重复...

public void print(ListOfItems storeList)
{
    // At this point make sure that "getCopy(ArrayList <Item> listToCopy)" creates a deep copy!
    ArrayList <Item> copyOfList = storeList.getCopy(storeList.getList());

    // For using this statement make sure that you override "equals" in the "Item" class!
    Set <Item> uniqueItems = new HashSet <Item> (copyOfList);

    for(Item item : uniqueItems)
    {
        // Code for usage of each single item
    }
}

...这只是解决方案的一种可行方法,但请确保覆盖 equals,并确保您的函数getCopy()创建复制!

感谢Coderino Javarino,当然必须覆盖equalshashCode方法 {{1}方法!

覆盖toString方法的一个选项...

equals

这里有一个创建深层副本的选项......

@Override public boolean equals(Object object)
{
    if(this == object)
    {
        return true;
    }

    if(object == null || getClass() != object.getClass())
    {
        return false;
    }

    Item item = (Item) object;

    return Objects.equals(this.name, item.name) &&
         this.identifier == item.identifier &&
         this.price == item.price;
}

答案 1 :(得分:1)

根据你的代码,我猜你的最终警察名单上错过了第6项和第3项。因为删除操作不正确。

在开始for循环中,3个变量的初始状态为:

  • originalItems:[1,2,3,4,5,6,6,6,2,3,3,3]
  • 副本:[1,2,3,4,5,6,6,6,2,3,3,3]
  • copyItems:[1,2,3,4,5,6,6,6,2,3,3,3]

在outter for-loop完成第6轮(i = 5)后,上述3个变量的状态:

  • originalItems:[1,2,3,4,5,6,6,6,2,3,3,3]
  • 副本:[1,2,3,4,5,6,2,3,3,3]&lt; - 两个项目6被删除,它按预期工作
  • copyItems:[1,2,3,4,5,6,6,6,2,3,3,3]

不幸的是,当i = 6且j = 7时,我们发现&#34;项目6&#34;再次重复,复制列表将其删除。 &lt; - 问题在这里。

我们绝对能够解释为什么&#34;项目3&#34;失去了同样的想法。它发生在i = 10,j = 11时。

如何解决?如果您仍想使用2 for-loop,则可以实施以下策略:

init copy list is empty
init originalitem as copy of your list
for item in originalItem
   isExist = false
   for copy list
       if item in copy
           isExist = true
           break
   if isExist = false
       copy add item 

但是,有很多方法可以有效地删除列表中的重复元素,而不是使用2个循环策略。

答案 2 :(得分:0)

    else
    {
        Object[] originalItems = list.toArray();
        ArrayList copy = new ArrayList(list.size());
        for (int i = 0; i < originalItems.length; i++)
        {
            Item item = (Item) originalItems[i];
            if (copy.contains(item) == false)
            {
                copy.add(item);
            }
        }