gpt4 book ai didi

java - 如何将同一个对象的不同实例添加到Java中的LinkedList中?

转载 作者:行者123 更新时间:2023-12-01 13:40:04 25 4
gpt4 key购买 nike

我在这里设计一个商店系统,它主要是一个媒体商店,包含视频游戏、DVD和CD等元素。我遇到的问题是我想将同一对象(在本例中为视频游戏)的不同实例存储到链接列表中。它似乎有效,但是当我输出列表时,它只输出输入到列表中的最后一个项目,并重复自身以获取列表中应该包含的对象数量。

我知道需要查看大量代码,但我们将非常感谢任何帮助。

以下是 addVideoGame 类的代码:

import java.util.*;
import java.io.*;

public class addVideoGame extends VideoGames implements java.io.Serializable{

public static VideoGames game = new VideoGames();
public static VideoGames eGame = new VideoGames();
public static LinkedList <VideoGames> games = new LinkedList<>();
private static int vgChoice = 0;
public static int vgCount = 0;
public static int vgAmount = 0;

public static void vgMenu(){
IBIO.output("1: Add a new videogame to the list.");
IBIO.output("2: View the contents of the list.");
IBIO.output("3: Save the contents of the list to the local area.");
IBIO.output("4: Load in data from a local file.");
IBIO.output("5: Return to the main menu.");
vgChoice = IBIO.inputInt("Make your choice: ");

if(vgChoice == 1){
vgAmount = IBIO.inputInt("How many games would you like to add to the database?: ");
for(int x = 0; x < vgAmount; x = x + 1){
VideoGames vg = new VideoGames();
vg.setTitle(IBIO.inputString("Enter the title of the game: "));
vg.setPublisher(IBIO.inputString("Enter the publisher of the game: "));
vg.setDeveloper(IBIO.inputString("Enter the developer of the game: "));
vg.setAgeRating(IBIO.inputInt("Enter the age rating of the game: "));
vg.setGenre(IBIO.inputString("Enter the genre of the game: "));
vg.setQuantity(IBIO.inputInt("Enter the available quantity of the game: "));
game = vg;
games.add(vg);
IBIO.output(" ");
}
vgMenu();
} else if(vgChoice == 2){
IBIO.output("Current amount of games in the list: " + games.size());
System.out.println(Arrays.toString(games.toArray()));
vgMenu();
} else if(vgChoice == 3){
try{
FileOutputStream fileOut = new FileOutputStream("I:\\IB\\Computer Science\\TheStore\\games.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(game);
out.close();
fileOut.close();
IBIO.output("Data has been saved to: I:\\IB\\Computer Science\\TheStore\\games.txt");
vgMenu();
} catch(IOException i){
i.printStackTrace();
}
} else if(vgChoice == 4){
eGame = null;
for(int x = 0; x < games.size(); x = x + 1){
try{
FileInputStream fileIn = new FileInputStream("I:\\IB\\Computer Science\\TheStore\\games.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
eGame = (VideoGames) in.readObject();
in.close();
fileIn.close();
} catch (IOException i){
i.printStackTrace();
return;
} catch(ClassNotFoundException c){
IBIO.output("VideoGames class not found");
c.printStackTrace();;
return;
}
IBIO.output("Object Details: " + eGame.toString());
vgMenu();
}
} else if(vgChoice == 5){
IBIO.output("Returning to main menu: ");
AccessMenus.adminMenu();
}
}

}

如果有人需要它们,这里是用于导航程序的两个接口(interface)类:

public class TheStore {

static String password; //Variable created to hold and check the value of password against the correct value.
public static boolean privilege = false; //Variable created to distinguish the difference between a normal user and a user with administrator privileges.

public static void main(String[] args) {
IBIO.output("Welcome to The Store!");
IBIO.output("Please make sure that you enter the correct password for your given privileges.");
password = IBIO.inputString("Enter password: ");
if(password.equalsIgnoreCase("admin")){ //Checks the entered value against the correct value.
privilege = true; //Sets global boolean value to true, so that admin access is granted.
IBIO.output(" ");
AccessMenus.adminMenu();//If password is correct, loads admin menu.
} else if(password.equalsIgnoreCase("user")){
privilege = false; //Keeps admin access off, so that unauthorised changes cannot be made.
IBIO.output(" ");
AccessMenus.userMenu();//If correct, loads user menu.
} else {
IBIO.output("The password is incorrect. Exiting program.");
System.exit(1); //If an incorrect password is entered, the program will close.
} //close else
}//close main
}//close class TheStore

第二个:

public class AccessMenus{

public static int choice;//Variable which will hold the value, which corresponds to an action depending on what value is entered.

public AccessMenus(){ //Null argument constructor, to set values to 0.
AccessMenus.choice = 0;
}

public AccessMenus(int c){ //Single argument constructor.
AccessMenus.choice = c;
}

public static void userMenu(){
IBIO.output("1: Sell a product.");
IBIO.output("2: Register a customer in the Loyalty programme.");
IBIO.output("3: Stock check.");
IBIO.output("4: Log out.");
IBIO.output(" ");
IBIO.output("Please make your choice: ");
choice = IBIO.inputInt();
if(choice == 1){
//Go to Sales class.
} else if(choice == 2){
//Go to CustomerRegister class.
} else if(choice == 3){
//Open the StockCheck class.
} else if(choice == 4){
IBIO.output("Logging out.");
System.exit(1);
} else {
IBIO.output("Invalid choice. Returning to menu.");
userMenu(); //If the value entered does not correspond to any action, the program will treat it as invalid and return to the menu.
}//close else
}//close userMenu

public static void adminMenu(){
IBIO.output("1: Sell a product.");
IBIO.output("2: Go to the specific object menus.");
IBIO.output("3: Stock check.");
IBIO.output("4: Order more stock.");
IBIO.output("5: Register a customer in the Loyalty programme.");
IBIO.output("6: Check Loyalty members.");
IBIO.output("7: Create databases.");
IBIO.output("8: Log out.");
IBIO.output(" ");
IBIO.output("Please make your choice: ");
choice = IBIO.inputInt();
if(choice == 1){
//Go to Sales class.
} else if(choice == 2){
int createChoice = 0;
IBIO.output("1: Video Games.");
IBIO.output("2: DVD.");
IBIO.output("3: CD.");
createChoice = IBIO.inputInt("Enter choice: ");
if(createChoice == 1){
addVideoGame.vgMenu();
} else if(createChoice == 2){
//Go to addDVD class.
} else if(createChoice == 3){
//Go to addCD class.
} else {
IBIO.output("Invalid input.");
adminMenu();
}
} else if(choice == 3){
//Go to StockCheck class.
} else if(choice == 4){
//Go to StockOrder class.
} else if(choice == 5){
//Go to CustomerRegister class.
} else if(choice == 6){
//Go to LoyaltyCheck class.
} else if(choice == 7){
//Go to DatabaseCreation class.
} else if(choice == 8){
IBIO.output("Logging out.");
System.exit(1);
} else {
IBIO.output("Invalid input. Returning to menu.");
adminMenu();
} //end else
}//close AdminMenu
}//close AccessMenus

此外,这是 VideoGames 对象类,包含访问器和修改器方法以及主要字段等内容:

public class VideoGames implements java.io.Serializable {
//Instance variables
public static String title;
public static int ageRating;
public static String genre;
public static String publisher;
public static String developer;
public static int quantity;

public VideoGames(){ //null argument constructor
VideoGames.title = null;
VideoGames.ageRating = 0;
VideoGames.genre = null;
VideoGames.publisher = null;
VideoGames.developer = null;
VideoGames.quantity = 0;
}//end VideoGames null argument constructor

public VideoGames(String t, int aG, String g, String p, String d, int q){ //6-argument constructor
VideoGames.title = t;
VideoGames.ageRating = aG;
VideoGames.genre = g;
VideoGames.publisher = p;
VideoGames.developer = d;
VideoGames.quantity = q;
}//end VideoGames 6-arguement constructor

public VideoGames(VideoGames game){
game = new VideoGames();
}

@Override
public String toString(){
return "\nTitle: " + title + " " + "\nPublisher: " + publisher + " " + "\nDeveloper: " + developer + " " + "\nAge Rating: " + ageRating + " " + "\nGenre: " + genre + " " + "\nQuantity: " + quantity + "\n ";
}

//Accessor and Mutator methods
public static String getTitle(){
return title;
}

public static void setTitle(String t){
title = t;
}

public static int getAgeRating(){
return ageRating;
}

public static void setAgeRating(int ag){
ageRating = ag;
}

public static String getGenre(){
return genre;
}

public static void setGenre(String g){
genre = g;
}

public static String getPublisher(){
return publisher;
}

public static void setPublisher(String p){
publisher = p;
}

public static void setDeveloper(String d){
developer = d;
}
public static String getDeveloper(){
return developer;
}
public static void setQuantity(int q){
quantity = q;
}

public static int getQuantity(){
return quantity;
}//end method setDeveloper
}//end class VideoGames

再次强调,我们将不胜感激任何帮助。

最佳答案

代码太多,我无法正确浏览,但听起来(快速扫描似乎证实了)您只创建了一个 VideoGames 对象。每当您更改该对象中的任何字段时,它都会更改该单个对象中的字段。

然后,您可以多次将同一个 VideoGames 对象添加到列表中,但这些都是对同一对象的单独引用。

相反,VideoGames 应该被称为 VideoGame 并包含一款游戏的数据,然后您应该创建一个 new VideoGame() 并进行设置每次您将一个添加到列表中时。

请记住,该列表仅包含对对象的引用,而不包含对象本身。

每个 VideoGames 对象就像街道上的房子。目前,您 build 一栋房屋并继续将该房屋的门涂成不同的颜色,而不是 build 多栋房屋并将每栋房屋的门涂成不同的颜色。

当您添加同一所房子 4 次时,您的列表只是一个地址列表,该列表只是重复相同的地址。

所以你正在做:

在地 block 1 build 房屋

  1. 把房子的门漆成蓝色
  2. 在列表中写下图 1
  3. 把房子的门漆成绿色
  4. 再次将图 1 添加到列表中
  5. 把房子的门漆成红色
  6. 再次将图 1 添加到列表中

现在您查看列表,您会看到三个条目 - 但当您查看门颜色时,它们都显示为红色。

关于java - 如何将同一个对象的不同实例添加到Java中的LinkedList中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20904872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com