我遇到了一个问题,当我在addLaptop()方法中合并两个数组“laptop”和“arr2”,并创建第三个数组“newArray”以包含“laptop”和“arr2”的值,然后将“laptop”数组设置为等于“newArray”的值,并在addLaptop()方法中打印“laptop”时,“laptop”的值将等于“newArray”,正如我所希望的那样。
然而,当我尝试从我的printAllLaptops()方法中获取“笔记本电脑”中的数据时,“笔记本电脑”数组中的值被设置回其原始值,而不是像我希望的那样被设置为“newArray”的值。
这里我的问题是什么?我不知道为什么值不会更新?我已经在这个问题上停留了几个小时,尝试将'laptop'数组移动到我的不同方法中,尝试设置laptop = newArray,还尝试从我的addLaptop()方法中以几种不同的方式返回laptop。
**调用我的方法的代码:LaptopFinderApp.java
package docComments;
import java.util.Scanner;
public class LaptopFinderApp {
public static void main(String[] args) {
int loop = 0;
while (loop !=1) {
String userInput = null;
// Entering 1 is what calls the method I am having issues with
System.out.println("1. Show all laptops");
// Entering 2 is what calls the method that updates my 'laptops Array'
System.out.println("2. Add a laptop");
System.out.println("3. Find a laptop");
System.out.println("4. Delete a laptop");
System.out.println("5. Number of laptops");
System.out.println("6. Exit");
System.out.println("Enter your selection:" );
Scanner myObj = new Scanner(System.in);
userInput = myObj.nextLine(); // Read user input
System.out.println();
// Converts user input from a string to an integer
int convertedInput = Integer.parseInt(userInput);
// Handels user inputs
if (convertedInput > 6) {
System.out.println("Enter a selection 1 - 6");
} else if (convertedInput == 6) {
System.out.println("Goodbye");
break;
} else if (convertedInput == 5) {
} else if (convertedInput == 4) {
} else if (convertedInput == 3) {
} else if (convertedInput == 2) {
System.out.println("GPU:");
String cpu = myObj.nextLine();
System.out.println("CPU:");
String gpu = myObj.nextLine();
System.out.println("Battery Life:");
String batterylife = myObj.nextLine();
Laptops addLaptop = new Laptops(gpu, cpu, batterylife);
addLaptop.addLaptop();
} else if (convertedInput == 1) {
Laptops name = new Laptops(null, null, null);
name.printAllLaptops();
} else if (convertedInput < 1) {
System.out.println("Enter a selection 1 - 6");
} else {
System.out.println("Error please try again.");
}
System.out.println();
}
}
}
**我的问题代码:Laptops.java
package docComments;
import java.util.Arrays;
public class Laptops {
/**
* Needs to have GPU, CPU, Battery Life, unique id and static count as attributes.
*/
private String gpu;
private String cpu;
private String batterylife;
private int id;
private int counter;
public Laptops(String gpu, String cpu, String batterylife) {
this.gpu = gpu;
this.cpu = cpu;
this.batterylife = batterylife;
this.id = 1000003;
}
/**
* Returns the GPU of the Laptop.
* @return the GPU
*/
public String getGpu() {
return gpu;
}
/**
* Returns the CPU of the Laptop.
* @return the CPU
*/
public String getCpu() {
return cpu;
}
/**
* Returns the batterylife of the Laptop.
* @return the batterylife
*/
public String getBatteryLife() {
return batterylife;
}
/**
* Returns the user inputed id of the Laptop.
* @return the user inputed id
*/
public int getId() {
return id;
}
/**
* Returns the new id we created.
* @return the new id
*/
public int creatId() {
counter = counter + 1;
id = id + counter;
return id;
}
/**
* Array of laptops
*/
String[][] laptops = {
{"1000001", "RTX 3080", "Intel i7", "24h"},
{"1000002", "RTX 4090", "Intel i9", "16h"},
{"1000003", "GTX 1660", "Ryzen 5", "34h"}
};
/**
* Prints all of the laptops in our array
*/
public void printAllLaptops() {
System.out.println(Arrays.toString(laptops)); // only displays the three original laptops
for (int i = 0; i < laptops.length; ++i) {
System.out.println("Laptop " + i +": " + "ID:" + laptops[i][0] + " " + laptops[i][1] + " " + laptops[i][2] + " " + laptops[i][3]);
}
}
/**
* Adds user created laptop to laptops array
*/
public String[][] addLaptop() {
if (gpu != null) {
String arr2[][] = {{String.valueOf(creatId()), gpu, cpu, batterylife}};
// create new array
String newArray[][] = new String[laptops.length + arr2.length][];
// Copy laptops array to new array from 0 to laptops.length
System.arraycopy(laptops, 0, newArray, 0, laptops.length);
// copy second array to new array
System.arraycopy(arr2, 0, newArray, laptops.length, arr2.length);
// display all arrays
System.out.println("Array1 = " + Arrays.toString(laptops[0]));
System.out.println("Array2 = " + Arrays.toString(arr2[0]));
System.out.println("Merged Array = " + Arrays.toString(newArray[3]));
// set old array equal to new array
laptops = newArray;
return newArray;
} else {
System.out.println("Error adding laptop to list.");
return laptops;
}
}
/**
* Prints out a string that contains GPU, CPU, battery life and id.
*/
@Override
public String toString() {
return "GPU: " + gpu + " CPU: " + cpu + " Battery Life: " + batterylife + " ID: " + creatId();
}
}
1条答案
按热度按时间pjngdqdw1#
如果希望单个Laptops对象继续更新,那么应该在循环之前初始化它(我将示例从“addLaptop”重命名为“laptops”,以消除与同名函数的歧义)。
然后你想完全删除这一行(循环内部的一行,而不是循环上方的第一行),因为它是罪魁祸首:
你不需要在循环中重新初始化它,只需要调用addLaptop()(Laptop类的成员),但是要把你的3个参数gpu、cpu和batterylife传递给那个函数,所以当你调用那个方法时,它就像这样:
现在的情况是,您的代码不断地用
new
关键字覆盖旧的笔记本电脑。还需要注意的是,您的(input == 1)案例还应移除
new
笔记本电脑,并只需调用以便它更新您的Laptops示例的示例,而不是新示例。