繁体   English   中英

检查ArrayList中包含的对象中是否存在某个字符串,然后将该对象添加到另一个ArrayList中

[英]Checking if there is a certain string in an object contained in an ArrayList, then adding the object to another ArrayList

我有Arraylist的对象ArrayList<Product> productDatabase. 该对象包含一个String和一个double ,然后将这些对象通过addProductToDatabase();添加到productDatabase中addProductToDatabase(); 如下:

public void addProductToDatabase(String productName, double dimensions); {
    Product newProduct = new Product(ProductName, dimensions);
    productDatabase.add(newProduct);
}

我还想创建一个Arraylist<ProductCount> productInventory来计算占多少Product 但是,在可以将其添加到ArrayList<ProductCount> productInventory ,应首先在运行addProductToInventory()检查productDatabase中是否存在对象详细信息

public Product getProduct(String name) {
    for(i = 0; i < productDatabase.size(); i++)
    if(productDatabase.get(i).contains(name) //Error: cannot find symbol- method contains.(java.lang.String)
        return productDatabase.get(i)
}

public void addProductToInventory(String productName, double quantity)
{
    Product p = getProduct(name);        
    productCount.add(new ProductCount(o, quantity)); 
}

假设您始终具有不同的对象(因此,任何东西都不会具有相同的名称),但是您始终不确定尺寸(因此,当您输入相同的producttName +尺寸时,您将在其中编辑尺寸)。

最终,您必须将所有项目放入一个大盒子中并报告库存商品,因此,您还必须具有getProductQuantityTotal()getProductDimensionTotal() -顾名思义,get您已计算的对象总数,以及尺寸的总和。

我必须添加/更改/删除此代码的内容是什么? 不要先考虑语法(因为BlueJ会检查常见的语法错误,而我只是手动输入了此错误)。 我确定我在某处缺少for语句,并且可能滥用了contains()因为它无法识别它(我已经import java.util.*;import java.util.ArrayList;

要回答帖子标题中的问题:如何在对象中查找字符串,以获取这些对象的列表,下面是一些执行此操作的示例代码:

首先,我创建了一个具有字符串字段的普通对象:

class ObjectWithStringField  {
   private final String s;
   public ObjectWithStringField(String s)  {
      this.s = s;
   }
   public String getString()  {
      return  s;
   }
}

然后是一个代码,该代码填充列表,然后在每个字符串中搜索。 这里没有魔术,它只是遍历列表直到找到匹配项。

import  java.util.List;
import  java.util.Arrays;

/**
   <P>{@code java StringInObjectInList}</P>
 **/
public class StringInObjectInList  {
   public static final void main(String[] ignored)  {
      ObjectWithStringField[] owStrArr = new ObjectWithStringField[]  {
         new ObjectWithStringField("abc"),
         new ObjectWithStringField("def"),
         new ObjectWithStringField("ghi")};

      //Yes this is a List instead of an ArrayList, but you can easily 
      //change this to work with an ArrayList. I'll leave that to you  :)
      List<ObjectWithStringField> objWStrList = Arrays.asList(owStrArr);

      System.out.println("abc? " + doesStringInObjExistInList("abc", objWStrList));
      System.out.println("abcd? " + doesStringInObjExistInList("abcd", objWStrList));
   }
   private static final boolean doesStringInObjExistInList(String str_toFind, List<ObjectWithStringField> owStrList_toSearch)  {
      for(ObjectWithStringField owStr : owStrList_toSearch)  {
         if(owStr.getString().equals(str_toFind))  {
            return  true;
         }
      }
      return  false;
   }
}

输出:

[C:\java_code\]java StringInObjectInList
abc? true
abcd? false

在现实世界中,我将使用Map<String,ObjectWithStringField>而不是List ,而键该字段。 然后就像themap.containsKey("abc");一样简单themap.containsKey("abc"); 但是在这里可以根据需要实现。 要按照您的任务特别要求的工作,您仍然需要做很多工作,但是这应该可以让您有一个良好的开端。 祝好运!

暂无
暂无

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

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