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

Java 为什么我的循环没有检索最后的项目?,java,arrays,loops,object,arraylist,Java,Arrays,Loops,Object,Arraylist,我正在尝试实现一种方法来查看项目列表及其数量,而不会重复。我使用ArrayList来实现这一点,ArrayList将保存我创建的Item类型的对象。问题是在循环中,我从原始列表的副本中删除重复项,因为它不显示列表中的最后两项,我不知道如何修复它。这是代码。Item是非常简单的对象,它包含(int标识符、int价格、字符串名称) 好的,首先我建议使用一个集合来删除重复项 public void print(ListOfItems storeList) { // At this point

我正在尝试实现一种方法来查看项目列表及其数量,而不会重复。我使用ArrayList来实现这一点,ArrayList将保存我创建的Item类型的对象。问题是在循环中,我从原始列表的副本中删除重复项,因为它不显示列表中的最后两项,我不知道如何修复它。这是代码。Item是非常简单的对象,它包含(int标识符、int价格、字符串名称)


好的,首先我建议使用一个集合来删除重复项

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
    }
}
这里有一个创建深度副本的选项

public ArrayList getCopy(ArrayList <Item> listToCopy)
{
    if(null == listToCopy)
    {
        // Handle this option too
    }

    ArrayList <Item> copy = new ArrayList(listToCopy.size());

    for(Item item : listToCopy)
    {
        // It is important that your class "Item" contains a copy constructor
        copy.add(new Item(item));
    }

    return copy;
}
public ArrayList getCopy(ArrayList listToCopy)
{
if(null==listToCopy)
{
//也要处理这个选项
}
ArrayList copy=新的ArrayList(listToCopy.size());
用于(项目:列表复制)
{
//类“Item”包含一个复制构造函数是很重要的
复制。添加(新项目(项目));
}
返回副本;
}

好的,首先我建议使用一个集合来删除重复项

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
    }
}
这里有一个创建深度副本的选项

public ArrayList getCopy(ArrayList <Item> listToCopy)
{
    if(null == listToCopy)
    {
        // Handle this option too
    }

    ArrayList <Item> copy = new ArrayList(listToCopy.size());

    for(Item item : listToCopy)
    {
        // It is important that your class "Item" contains a copy constructor
        copy.add(new Item(item));
    }

    return copy;
}
public ArrayList getCopy(ArrayList listToCopy)
{
if(null==listToCopy)
{
//也要处理这个选项
}
ArrayList copy=新的ArrayList(listToCopy.size());
用于(项目:列表复制)
{
//类“Item”包含一个复制构造函数是很重要的
复制。添加(新项目(项目));
}
返回副本;
}

根据您的代码,我猜您的最终cop列表中遗漏了第6项和第3项。因为删除操作不正确

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

  • 原文:[1,2,3,4,5,6,6,6,2,3,3,3]
  • 副本:[1,2,3,4,5,6,6,6,2,3,3,3]
  • 复制项目:[1,2,3,4,5,6,6,6,2,3,3,3]
循环结束第6轮后,上述3个变量的状态(i=5):

  • 原文:[1,2,3,4,5,6,6,6,2,3,3,3]

  • 副本:[1,2,3,4,5,6,2,3,3]根据您的代码,我猜您的最终cop列表中遗漏了第6项和第3项。因为删除操作不正确

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

    • 原文:[1,2,3,4,5,6,6,6,2,3,3,3]
    • 副本:[1,2,3,4,5,6,6,6,2,3,3,3]
    • 复制项目:[1,2,3,4,5,6,6,6,2,3,3,3]
    循环结束第6轮后,上述3个变量的状态(i=5):

    • 原文:[1,2,3,4,5,6,6,6,2,3,3,3]
    • 副本:[1,2,3,4,5,6,2,3,3,3]
      else
      {
      Object[]originalItems=list.toArray();
      ArrayList copy=新的ArrayList(list.size());
      for(int i=0;i
      其他
      {
      Object[]originalItems=list.toArray();
      ArrayList copy=新的ArrayList(list.size());
      for(int i=0;i
      请将Item类添加到问题中另外,您确实不需要中间数组来删除重复项应该
      if(copyItems[i]!=null)
      be
      if(cop[i]!=null)
      ?是的,我编辑了这个问题。谢谢你注意到了。关于中间层,这是因为我不想弄乱原始列表。也不需要
      copyItems
      ,请将Item类添加到问题中。此外,如果(copyItems[i]!=null),你真的不需要中间数组来删除重复项
      be
      if(cop[i]!=null)
      ?是的,我编辑了这个问题。谢谢你注意到了。关于中间版本,这是因为我不想弄乱原始列表。也没有必要使用
      copyItems
      ,谢谢。我现在将查看哈希代码:)我仍在学习it@RedEyez听起来不错,但是hashCode本身并不能解决您的问题……您必须重写
      equals
      方法,建议也重写
      hashCode
      方法,但这不是解决此问题的部分谢谢。我现在将查看hashCode:)我仍在学习it@RedEyez听起来不错,但是
      hashCode
      本身并不能解决您的问题…您必须覆盖
      eqals
      方法,建议也重写
      hashCode
      方法,但这不是解决问题的部分谢谢你的建议,我重写了循环。把答案放在上面。谢谢你的建议,我重写了循环。把答案放在上面。
      @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;
      }
      
      public ArrayList getCopy(ArrayList <Item> listToCopy)
      {
          if(null == listToCopy)
          {
              // Handle this option too
          }
      
          ArrayList <Item> copy = new ArrayList(listToCopy.size());
      
          for(Item item : listToCopy)
          {
              // It is important that your class "Item" contains a copy constructor
              copy.add(new Item(item));
          }
      
          return copy;
      }
      
      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 
      
          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);
                  }
              }