Java 列表已断开,无法添加
代码最初由@Arvind编写。 我相信。 我会用I作为名词 我有四个类,商品,商店,顾客,在线 每当我在网上课堂上发现一个项目 我想从Store类通过addingitem()添加 我使用的链接,所以我可以存储,删除多个项目 但问题是,当我试图储存它时 我可以很好地使用debug to itemLists.add() 但是当尝试使用调试器查看项目列表中的内容时, 大小是零,里面什么也没有 我不知道为什么 我需要一个临时列表或其他东西来存储多个字符串。 我使用列表是因为它可以添加、删除和打印 假设我添加此项,因为它存在Java 列表已断开,无法添加,java,Java,代码最初由@Arvind编写。 我相信。 我会用I作为名词 我有四个类,商品,商店,顾客,在线 每当我在网上课堂上发现一个项目 我想从Store类通过addingitem()添加 我使用的链接,所以我可以存储,删除多个项目 但问题是,当我试图储存它时 我可以很好地使用debug to itemLists.add() 但是当尝试使用调试器查看项目列表中的内容时, 大小是零,里面什么也没有 我不知道为什么 我需要一个临时列表或其他东西来存储多个字符串。 我使用列表是因为它可以添加、删除和打印 假设我
001A:GTA
好,我再加一项,
001C:我们最后一个人
它不仅应该存储,而且当我想查看我存储的内容时。结果应该是
001A:GTA
001C:The last of us
有人能给我解释一下吗?
谢谢
import java.util.LinkedList;
导入java.util.Objects;
类项目{
私有字符串代码、名称;
公共项(字符串代码、字符串名称){
this.code=代码;
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共字符串getNumber(){
返回号码;
}
@凌驾
公共字符串toString(){
返回名称+“:”+代码;
}
@凌驾
公共int hashCode(){
返回Objects.hash(名称、代码);
}
@凌驾
公共布尔等于(对象obj){
项目=(项目)obj;
返回name.equals(item.name)和&code.equals(item.code);
}
}
班级商店{
私有LinkedList项=新建LinkedList();
私有字符串名称、编号;
公共存储(字符串名称、字符串编号){
this.name=名称;
这个数字=数字;
}
公共字符串getName(){
返回名称;
}
public void addItem(字符串代码、字符串名称){
添加(新项目(代码、名称));
}
公共链接列表findItems(){
退货项目;
}
@凌驾
公共字符串toString(){
StringBuilder sb=新的StringBuilder();
用于(项目:项目){
sb.append(item.toString()).append(System.lineSeparator());
}
使某人返回字符串();
}
公共列表添加项(项目){
List itemLists=new ArrayList();
itemsList.add(item.getName()+item.geNumber()+“added”);
返回null;
}
公共void viewStore(){
System.out.println(名称+“”+编号);
}
/**
*使用搜索字符串搜索商店中的项目
*
*@param以“code:name:”格式组合代码和名称的字符串
*@return返回匹配项,如果未找到匹配项,则返回null
*/
公共项findItem(字符串strItem){
字符串[]数据=strItem.split(“:”);
如果(数据长度<2){
返回null;
}
Item searchItem=新项目(数据[0],数据[1]);
用于(项目:项目){
if(item.equals(searchItem)){
退货项目;
}
}
返回null;
}
/**
*使用项目名称搜索存储中的项目
*
*@param项目的名称
*@return返回匹配项,如果未找到匹配项,则返回null
*/
公共项findItemByName(字符串itemName){
用于(项目:项目){
if(item.getName().equals(itemName)){
退货项目;
}
}
返回null;
}
}
类客户{
私有LinkedList存储=新建LinkedList();
公众客户(){
添加(新商店(“游戏”、“1”));
添加(新商店(“杂货店”、“2”));
存储。获取(0)。添加项(“001A”、“GTA”);
stores.get(0).addItem(“001B”,“战神”);
stores.get(0).addItem(“001C”,“我们的最后一个”);
商店。获取(1)。添加项(“002A”,“三明治”);
商店。获取(1)。添加项(“002B”,“杯面”);
商店。获取(1)。添加项(“002C”,“冰淇淋”);
}
公共链接列表getStores(){
退货商店;
}
公共void viewStore(){
适用于(门店:门店)
viewStore();
}
公共void viewItems(){
适用于(门店:门店){
对于(项:store.findItems()){
系统输出打印项次(项);
}
}
}
/**
*使用搜索在与客户相关的所有商店中搜索该商品
*串
*
*@param以“code:name:”格式组合代码和名称的字符串
*@return返回匹配项,如果未找到匹配项,则返回null
*/
公共项findItem(字符串strItem){
字符串[]数据=strItem.split(“:”);
如果(数据长度<2){
返回null;
}
Item searchItem=新项目(数据[0],数据[1]);
适用于(门店:门店){
对于(项:store.findItems()){
if(item.equals(searchItem)){
退货项目;
}
}
}
返回null;
}
/**
*使用的名称搜索与客户相关的所有门店中的商品
*项目
*
*@param名称o
import java.util.LinkedList;
import java.util.Objects;
class Item {
private String code, name;
public Item(String code, String name) {
this.code = code;
this.name = name;
}
public String getName() {
return name;
}
public String getNumber(){
return number;
}
@Override
public String toString() {
return name + " : " + code;
}
@Override
public int hashCode() {
return Objects.hash(name, code);
}
@Override
public boolean equals(Object obj) {
Item item = (Item) obj;
return name.equals(item.name) && code.equals(item.code);
}
}
class Store {
private LinkedList<Item> items = new LinkedList<Item>();
private String name, number;
public Store(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void addItem(String code, String name) {
items.add(new Item(code, name));
}
public LinkedList<Item> findItems() {
return items;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Item item : items) {
sb.append(item.toString()).append(System.lineSeparator());
}
return sb.toString();
}
public List<String> addingitem(Item item){
List<String> itemLists = new ArrayList<>();
itemsList.add(item.getName() + item.geNumber() + “ added”);
return null;
}
public void viewStore() {
System.out.println(name + " " + number);
}
/**
* Searches the item in the store using the search string
*
* @param The string combining the code and name in the format "code:name:"
* @return Returns the matching Item. If the item is not found, null is returned
*/
public Item findItem(String strItem) {
String[] data = strItem.split(":");
if (data.length < 2) {
return null;
}
Item searchItem = new Item(data[0], data[1]);
for (Item item : items) {
if (item.equals(searchItem)) {
return item;
}
}
return null;
}
/**
* Searches the item in the store using the name of the item
*
* @param The name of the item
* @return Returns the matching Item. If the item is not found, null is returned
*/
public Item findItemByName(String itemName) {
for (Item item : items) {
if (item.getName().equals(itemName)) {
return item;
}
}
return null;
}
}
class Customer {
private LinkedList<Store> stores = new LinkedList<Store>();
public Customer() {
stores.add(new Store("Game", "1"));
stores.add(new Store("Grocery", "2"));
stores.get(0).addItem("001A", "GTA");
stores.get(0).addItem("001B", "GOD OF WARS");
stores.get(0).addItem("001C", "THE LAST OF US");
stores.get(1).addItem("002A", "Sandwich");
stores.get(1).addItem("002B", "Cup Noodle");
stores.get(1).addItem("002C", "Ice Cream");
}
public LinkedList<Store> getStores() {
return stores;
}
public void viewStore() {
for (Store store : stores)
store.viewStore();
}
public void viewItems() {
for (Store store : stores) {
for (Item item : store.findItems()) {
System.out.println(item);
}
}
}
/**
* Searches the item in all the stores related to the customer using the search
* string
*
* @param The string combining the code and name in the format "code:name:"
* @return Returns the matching Item. If the item is not found, null is returned
*/
public Item findItem(String strItem) {
String[] data = strItem.split(":");
if (data.length < 2) {
return null;
}
Item searchItem = new Item(data[0], data[1]);
for (Store store : stores) {
for (Item item : store.findItems()) {
if (item.equals(searchItem)) {
return item;
}
}
}
return null;
}
/**
* Searches the item in all the stores related to the customer using the name of
* the item
*
* @param The name of the item
* @return Returns the matching Item. If the item is not found, null is returned
*/
public Item findItemByName(String itemName) {
for (Store store : stores) {
for (Item item : store.findItems()) {
if (item.getName().equals(itemName)) {
return item;
}
}
}
return null;
}
}
public class Online {
public static void main(String[] args) {
Item item;
String strItem, itemName;
Customer customer = new Customer();
// Test searching the item using the search string in a particular store e.g.
// first store of customer
Store store = customer.getStores().get(0);
System.out.println("Searching item using search string in store, " + store.getName());
strItem = "001A:GTA";
item = store.findItem(strItem);
if (item == null) {
System.out.println("Not found -> " + strItem);
} else {
System.out.println("Found -> " + item);
store.addingItem(item);
}
public List<String> addingitem(Item item){
List<String> itemLists = new ArrayList<>();
itemsList.add(item.getName() + item.geNumber() + “ added”);
return null;
}
public List<String> addingitem(Item item){
return null;
}
public List<String> addingitem(Item item){
this.items.add(item); // add the given item to the list of items of the store.
return null;
}
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
class Item {
private String code, name;
public Item(String code, String name) {
this.code = code;
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name + " : " + code;
}
@Override
public int hashCode() {
return Objects.hash(name, code);
}
@Override
public boolean equals(Object obj) {
Item item = (Item) obj;
return name.equals(item.name) && code.equals(item.code);
}
}
class Store {
private LinkedList<Item> items = new LinkedList<Item>();
private String name, number;
public Store(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void addItem(String code, String name) {
items.add(new Item(code, name));
}
public void addItem(String strItem) {
String[] data = strItem.split(":");
if (data.length < 2) {
throw new IllegalArgumentException("Item string must be in the format code:name");
}
items.add(new Item(data[0], data[1]));
}
public LinkedList<Item> findItems() {
return items;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Item item : items) {
sb.append(item.toString()).append(System.lineSeparator());
}
return sb.toString();
}
public void viewStore() {
System.out.println(name + " " + number);
}
/**
* Searches the item in the store using the search string
*
* @param The string combining the code and name in the format "code:name:"
* @return Returns the matching Item. If the item is not found, null is returned
*/
public Item findItem(String strItem) {
String[] data = strItem.split(":");
if (data.length < 2) {
return null;
}
Item searchItem = new Item(data[0], data[1]);
for (Item item : items) {
if (item.equals(searchItem)) {
return item;
}
}
return null;
}
/**
* Searches the item in the store using the name of the item
*
* @param The name of the item
* @return Returns the matching Item. If the item is not found, null is returned
*/
public Item findItemByName(String itemName) {
for (Item item : items) {
if (item.getName().equals(itemName)) {
return item;
}
}
return null;
}
}
class Customer {
private LinkedList<Store> stores = new LinkedList<Store>();
public Customer() {
stores.add(new Store("Game", "1"));
stores.add(new Store("Grocery", "2"));
stores.get(0).addItem("001A", "GTA");
stores.get(0).addItem("001B", "GOD OF WARS");
stores.get(0).addItem("001C", "THE LAST OF US");
stores.get(1).addItem("002A", "Sandwich");
stores.get(1).addItem("002B", "Cup Noodle");
stores.get(1).addItem("002C", "Ice Cream");
}
public LinkedList<Store> getStores() {
return stores;
}
public void viewStore() {
for (Store store : stores)
store.viewStore();
}
public void viewItems() {
for (Store store : stores) {
for (Item item : store.findItems()) {
System.out.println(item);
}
}
}
/**
* Searches the item in all the stores related to the customer using the search
* string
*
* @param The string combining the code and name in the format "code:name:"
* @return Returns the matching Item. If the item is not found, null is returned
*/
public Item findItem(String strItem) {
String[] data = strItem.split(":");
if (data.length < 2) {
return null;
}
Item searchItem = new Item(data[0], data[1]);
for (Store store : stores) {
for (Item item : store.findItems()) {
if (item.equals(searchItem)) {
return item;
}
}
}
return null;
}
/**
* Searches the item in all the stores related to the customer using the name of
* the item
*
* @param The name of the item
* @return Returns the matching Item. If the item is not found, null is returned
*/
public Item findItemByName(String itemName) {
for (Store store : stores) {
for (Item item : store.findItems()) {
if (item.getName().equals(itemName)) {
return item;
}
}
}
return null;
}
}
public class Online {
public static void main(String[] args) {
Item item;
String strItem, itemName;
Customer customer = new Customer();
// List to hold the items which are found
List<Item> itemsList = new LinkedList<Item>();
// Test searching the item using the search string in a particular store e.g.
// first store of customer
Store store = customer.getStores().get(0);
System.out.println("Searching item using search string in store, " + store.getName());
strItem = "001A:GTA";
item = store.findItem(strItem);
if (item != null) {
itemsList.add(item);
} else {
System.out.println("'" + strItem + "'" + " was not found in in store, " + store.getName());
}
strItem = "001D:The Wizarding World of Harry Potter";
item = store.findItem(strItem);
if (item != null) {
itemsList.add(item);
} else {
System.out.println("'" + strItem + "'" + " was not found in in store, " + store.getName());
}
strItem = "001B:GOD OF WARS";
item = store.findItem(strItem);
if (item != null) {
itemsList.add(item);
} else {
System.out.println("'" + strItem + "'" + " was not found in in store, " + store.getName());
}
strItem = "ABC:XYZ";
item = store.findItem(strItem);
if (item != null) {
itemsList.add(item);
} else {
System.out.println("'" + strItem + "'" + " was not found in in store, " + store.getName());
}
System.out.println();
System.out.println("Items which have been found:");
itemsList.stream().forEach(System.out::println);
System.out.println();
// Test adding "ABC:XYZ" to store, Game
strItem = "ABC:XYZ";
try {
store.addItem(strItem);
System.out.println("Item, '" + strItem + "' has been added to the store successfully.");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
// Test finding the newly added item to the store
strItem = "ABC:XYZ";
item = store.findItem(strItem);
if (item != null) {
itemsList.add(item);
} else {
System.out.println("'" + strItem + "'" + " was not found in in store, " + store.getName());
}
System.out.println();
System.out.println("Updated list of items which have been found:");
itemsList.stream().forEach(System.out::println);
}
}
Searching item using search string in store, Game
'001D:The Wizarding World of Harry Potter' was not found in in store, Game
'ABC:XYZ' was not found in in store, Game
Items which have been found:
GTA : 001A
GOD OF WARS : 001B
Item, 'ABC:XYZ' has been added to the store successfully.
Updated list of items which have been found:
GTA : 001A
GOD OF WARS : 001B
XYZ : ABC