[英]Checking if there is a certain string in an object contained in an ArrayList, then adding the object to another ArrayList
I have Arraylist
of objects ArrayList<Product> productDatabase.
我有
Arraylist
的对象ArrayList<Product> productDatabase.
The object contains a String
and a double
and then these objects will be added to the productDatabase by addProductToDatabase();
该对象包含一个
String
和一个double
,然后将这些对象通过addProductToDatabase();
添加到productDatabase中addProductToDatabase();
as follows: 如下:
public void addProductToDatabase(String productName, double dimensions); {
Product newProduct = new Product(ProductName, dimensions);
productDatabase.add(newProduct);
}
I also want to make an Arraylist<ProductCount> productInventory
which counts how many Product
are accounted for. 我还想创建一个
Arraylist<ProductCount> productInventory
来计算占多少Product
。 Before it can add to ArrayList<ProductCount> productInventory
however, it should first check if the object details exist in the productDatabase while running addProductToInventory()
但是,在可以将其添加到
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));
}
Assume that you always have different objects (so nothing will have the same name), but you're always unsure of the dimensions (so when you input the same producttName
+ dimensions you edit the dimensions in it). 假设您始终具有不同的对象(因此,任何东西都不会具有相同的名称),但是您始终不确定尺寸(因此,当您输入相同的
producttName
+尺寸时,您将在其中编辑尺寸)。
At the end of the day, you have to put all the items in it a large box and report what you've inventoried, so you also have a getProductQuantityTotal()
and you have to getProductDimensionTotal()
-- as the name suggests, get the total of number of objects you've counted, and the sum of the dimensions. 最终,您必须将所有项目放入一个大盒子中并报告库存商品,因此,您还必须具有
getProductQuantityTotal()
和getProductDimensionTotal()
-顾名思义,get您已计算的对象总数,以及尺寸的总和。
What do I have to add/change/remove about this code? 我必须添加/更改/删除此代码的内容是什么? Don't consider syntax first (because BlueJ checks for common syntax errors and I just typed this by hand).
不要先考虑语法(因为BlueJ会检查常见的语法错误,而我只是手动输入了此错误)。 I'm sure that I'm missing a
for
statement somewhere, and I'm probably misusing contains()
because it won't recognise it (I have import java.util.*;
and import java.util.ArrayList;
) 我确定我在某处缺少
for
语句,并且可能滥用了contains()
因为它无法识别它(我已经import java.util.*;
和import java.util.ArrayList;
)
To answer the question in your post title: How to find a string in an object , for a list of those objects, here is some sample code that does this: 要回答帖子标题中的问题:如何在对象中查找字符串,以获取这些对象的列表,下面是一些执行此操作的示例代码:
First, I created a trivial object that has a string field: 首先,我创建了一个具有字符串字段的普通对象:
class ObjectWithStringField {
private final String s;
public ObjectWithStringField(String s) {
this.s = s;
}
public String getString() {
return s;
}
}
And then a code that populates a list of it, and then searches each for the string. 然后是一个代码,该代码填充列表,然后在每个字符串中搜索。 There's no magic here, it just iterates through the list until a match is found.
这里没有魔术,它只是遍历列表直到找到匹配项。
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;
}
}
Output: 输出:
[C:\java_code\]java StringInObjectInList
abc? true
abcd? false
In the real world, instead of a List
, I'd use a Map<String,ObjectWithStringField>
, where the key is that field. 在现实世界中,我将使用
Map<String,ObjectWithStringField>
而不是List
,而键是该字段。 Then it'd be as simple as themap.containsKey("abc");
然后就像
themap.containsKey("abc");
一样简单themap.containsKey("abc");
. 。 But here it is implemented as you require.
但是在这里可以根据需要实现。 You'll still have quite a bit of work to do, to get this working as specifically required by your assignment, but it should get you off to a good start.
要按照您的任务特别要求的工作,您仍然需要做很多工作,但是这应该可以让您有一个良好的开端。 Good luck!
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.