【发布时间】:2018-05-15 08:49:33
【问题描述】:
假设我有以下Class Product:
public class Product {
// Variables.
private String name; // Name
private Double price; // Price
Product() {} // Default constructor with no parameters.
Product(String name, Double price) { // Constructor with parameters.
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price= price;
}
public String toString() { // Overriding "toString()".
return "\nName: " + this.name + "\nPrice: " + this.price;
}
public boolean equals(Object obj) { // Overriding equals()
if(this == obj) {
return true;
}
if(obj == null || obj.getClass() != this.getClass()) {
return false;
}
Product product = (Product) obj;
return this.name.equals(product.name)&& this.price.equals(product.price);
}
}
现在,假设我的Main.class 中有一个ArrayList,而我的Main 看起来像这样:
import java.util.*;
import java.io.*;
public class Main {
private static BufferedReader r = new BufferedReader (new InputStreamReader(System.in));
private static String readln() throws IOException{
return r.readLine();
}
private static long readInput() throws IOException{ // Use this to read input for the menu options.
return Integer.valueOf(readln());
}
public static void menu(){ // Menu
System.out.println("-------------------------" +
"\nAdd new product(1)" +
"\nSearch for product(2)" +
"\nDelete product(3)" +
"\nShow all products(4)" +
"\nReturn the number of products(5)" +
"\nExit(-1)" +
"\n-------------------------");
}
public static void main (String args[]) throws IOException{
// This is the ArrayList for the "Product".
ArrayList<Product> products = new ArrayList<Product>();
int option = 0;
do {
menu();
option = (int)readInput();
switch (option){
case 1:{
System.out.println("Insert product name: ");
String name= readln();
System.out.println("Insert product price: ");
Double price = Double.parseDouble(readln());
products.add(new Product(name, price));
break;
}
case 2:{
System.out.println("Insert product name: ");
String name= readln();
System.out.println("Insert product price: ");
Double price= Double.parseDouble(readln());
if ((products.contains(new Product (name, price)))){
System.out.println("Works!");
}
break;
}
case 3:{
break;
}
case 4:{
break;
}
case 5:{
System.out.println("Number of products: " + products.size());
//This prints with no problems, therefor the objects DO exist in the ArrayList.
break;
}
}
}while((option > 0) && (option < 6));
}
}
根据this,为了在ArrayList中插入一个对象,你需要这样写“ArrayListName.add(new ObjectName(param1, param2));”或者你可以创建一个名为object1的对象,然后将它与ArrayListName.add(object1);添加到我的情况,据我了解,我将对象插入到 ArrayList 但这些对象并不真正存在,因为如果我尝试使用覆盖的 toString() 方法,它不会打印任何内容。 如果我理解错了,为什么不打印?根据this,我的方法是正确的。
如果我正确理解this,对象不需要变量来指向它们,但如果你像我一样直接将它们插入ArrayList,你应该怎么做获取对象的索引位置? 因为在我的例子中equals() 比较对象,所以你不能用它来搜索ArrayList。你也不能尝试像“products.contains(name, price);”这样的东西,因为.contains()使用equals()。
我也在考虑做类似this 的事情,但它只有在你想创建一个新的Class 而不是像product1 这样的对象时才有用,就我而言。我也放弃了它,因为forName() 一直说它找不到Class,因为我找不到原因。
“删除”选项怎么样?它会和“搜索”一样吗?
编辑:对于equals()最后一行,也可以放:
if( (this.price.equals(product.getPrice())) && (this.name.equals(product.getName())) ) {
return true;
}
【问题讨论】:
-
一次问一个问题怎么样?你在哪里打电话
toString()? -
这是打印数组列表第一个元素的示例:
System.out.println(products.get(0))。它将调用您的自定义toString()方法。我猜你想在用户选择第四个菜单选项时循环播放每个产品并打印它们。 -
@MuratK。我不。 “.....因为如果我尝试使用被覆盖的 toString() 方法......”
-
@Oneiros 当我尝试从
ArrayList打印东西时,它可以工作,但问题是,当你没有指向它的变量时,你将如何搜索它以查找对象,假设您也不知道该对象的索引?