基本上我的教授告诉我要使用arraylist?我不明白他的观点是诚实的..我认为他希望我将对象添加到arraylist?现在,我真的不知道该怎么做。
我的代码正在运行,真的很好。但是,他仍然希望我利用arraylist使其看起来更好?
/ * *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|范本 *并在编辑器中打开模板。 * / 软件包ahaprogram2;
import java.util.ArrayList;
import java.util.Scanner;
public class AhaProgram {
public static Container container1 = new Container("Container 1: ");
public static Container container2 = new Container("Container 2: ");
public static Container container3 = new Container("Container 3: ");
public static Container container4 = new Container("Container 4: ");
public static Container container5 = new Container("Container 5: ");
public static boolean loop = false;
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Hello! This is the AHA Program of Jalosjos,
Parreno and Alfonso");
System.out.println("Please type the letter of your option");
while (loop != true) {
showOptions();
InputHandler();
}
}
public static void InputHandler() {
Scanner reader = new Scanner(System.in);
String optionletter = reader.nextLine();
if (optionletter.equals("A")) { // OPTION A
System.out.println("There are 5 containers.. What container
will you name? ");
System.out.print("Type the number of your container: ");
String contInput = reader.nextLine();
if (contInput.equals("1")) {
System.out.print("Input the name of Container 1: ");
String ContInp1 = reader.nextLine();
container1.renameCont(ContInp1);
container1.printContainer();
} else if (contInput.equals("2")) {
System.out.print("Input the name of Container 2: ");
String ContInp2 = reader.nextLine();
container2.renameCont(ContInp2);
container2.printContainer();
} else if (contInput.equals("3")) {
System.out.print("Input the name of Container 3: ");
String ContInp3 = reader.nextLine();
container3.renameCont(ContInp3);
container3.printContainer();
} else if (contInput.equals("4")) {
System.out.print("Input the name of Container 4: ");
String ContInp4 = reader.nextLine();
container4.renameCont(ContInp4);
container4.printContainer();
} else if (contInput.equals("5")) {
System.out.print("Input the name of Container 5: ");
String ContInp5 = reader.nextLine();
container5.renameCont(ContInp5);
container5.printContainer();
}
}
if (optionletter.equals("B")) { // for option B
System.out.println("Which container will you use?");
System.out.print("Type a number for the container: ");
String contforAdd = reader.nextLine();
if (contforAdd.equals("1")) {
System.out.print("How many cans will you add?: ");
int numofCans1 = Integer.parseInt(reader.nextLine());
for (int i = 0; i < numofCans1; i++) {
System.out.print("Enter the name of Can " + (i + 1) +
" : ");
String CanName = reader.nextLine();
container1.AddCan(CanName);
}
System.out.println("**CANS ADDED SUCCESSFULLY**");
}
if (contforAdd.equals("2")) {
System.out.print("How many cans will you add?: ");
int numofCans2 = Integer.parseInt(reader.nextLine());
for (int i = 0; i < numofCans2; i++) {
System.out.print("Enter the name of Can " + (i + 1) +
" : ");
String CanName = reader.nextLine();
container2.AddCan(CanName);
}
System.out.println("**CANS ADDED SUCCESSFULLY**");
}
if (contforAdd.equals("3")) {
System.out.print("How many cans will you add?: ");
int numofCans3 = Integer.parseInt(reader.nextLine());
for (int i = 0; i < numofCans3; i++) {
System.out.print("Enter the name of Can " + (i + 1) +
" : ");
String CanName = reader.nextLine();
container3.AddCan(CanName);
}
System.out.println("**CANS ADDED SUCCESSFULLY**");
}
if (contforAdd.equals("4")) {
System.out.print("How many cans will you add?: ");
int numofCans4 = Integer.parseInt(reader.nextLine());
for (int i = 0; i < numofCans4; i++) {
System.out.print("Enter the name of Can " + (i + 1) +
" : ");
String CanName = reader.nextLine();
container4.AddCan(CanName);
}
System.out.println("**CANS ADDED SUCCESSFULLY**");
}
if (contforAdd.equals("5")) {
System.out.print("How many cans will you add?: ");
int numofCans5 = Integer.parseInt(reader.nextLine());
for (int i = 0; i < numofCans5; i++) {
System.out.print("Enter the name of Can " + (i + 1) +
" : ");
String CanName = reader.nextLine();
container5.AddCan(CanName);
}
System.out.println("**CANS ADDED SUCCESSFULLY**");
}
}
if (optionletter.equals("C")) {
System.out.println("Which container will you use?");
System.out.print("Type a number for the container: ");
String contforRemove = reader.nextLine();
if (contforRemove.equals("1")) {
System.out.print("What can will you remove?: ");
String canRemove = reader.nextLine();
container1.RemoveCan(canRemove);
}
if (contforRemove.equals("2")) {
System.out.print("What can will you remove?: ");
String canRemove = reader.nextLine();
container2.RemoveCan(canRemove);
}
if (contforRemove.equals("3")) {
System.out.print("What can will you remove?: ");
String canRemove = reader.nextLine();
container3.RemoveCan(canRemove);
}
if (contforRemove.equals("4")) {
System.out.print("What can will you remove?: ");
String canRemove = reader.nextLine();
container4.RemoveCan(canRemove);
}
if (contforRemove.equals("5")) {
System.out.print("What can will you remove?: ");
String canRemove = reader.nextLine();
container5.RemoveCan(canRemove);
}
}
if (optionletter.equals("D")) {
showOptionsDisplay();
System.out.print("Type a letter: ");
String letterDisplay = reader.nextLine();
if (letterDisplay.equals("A")) {
container1.printContents();
System.out.println("");
}
if (letterDisplay.equals("B")) {
container2.printContents();
System.out.println("");
}
if (letterDisplay.equals("C")) {
container3.printContents();
System.out.println("");
}
if (letterDisplay.equals("D")) {
container4.printContents();
System.out.println("");
}
if (letterDisplay.equals("E")) {
container5.printContents();
System.out.println("");
}
if (letterDisplay.equals("F")) {
container1.printContents();
System.out.println("");
container2.printContents();
System.out.println("");
container3.printContents();
System.out.println("");
container4.printContents();
System.out.println("");
container5.printContents();
System.out.println("");
}
}
if (optionletter.equals("E")) {
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Thank you for using our program. MWAH!");
loop = true;
}
}
public static void showOptions() {
System.out.println("A = Name Containers");
System.out.println("B = Add Cans");
System.out.println("C = Remove Cans");
System.out.println("D = Display Cans");
System.out.println("E = Quit");
System.out.print("Type a Letter: ");
}
public static void showOptionsDisplay() {
System.out.println("Pick an Option");
System.out.println("A = Display container 1");
System.out.println("B = Display container 2");
System.out.println("C = Display container 3");
System.out.println("D = Display container 4");
System.out.println("E = Display container 5");
System.out.println("F = Display all containers");
}
}
这是另一个班级
package ahaprogram2;
import java.util.ArrayList;
import java.util.Scanner;
public class Container {
Scanner reader = new Scanner(System.in);
public ArrayList<String> CanContainer = new ArrayList<String>();
public int Contsizep;
public String contName;
public String changeName;
public Container(String contname){
this.contName = contname;
}
public void AddCan(String CantoAdd) {
this.CanContainer.add(CantoAdd);
}
public void RemoveCan(String CantoRemove) {
if (this.CanContainer.contains(CantoRemove)) {
this.CanContainer.remove(CantoRemove);
System.out.println("** " + CantoRemove + " Can removed
successfully**");
}
else {
System.out.println("Can cannot be found.. make sure to put the
exact name!!");
}
}
public void renameCont(String changename) {
this.contName += changename;
}
public void printContents() {
System.out.println("Here are the contents of " + contName);
System.out.println("");
for(String counter : this.CanContainer){
System.out.println(counter); }
}
public void printContainer() { // for OPTION A ONLY
System.out.println("CONTAINER NAME SUCCESSFUL: ** " + contName +
"**");
}
}
我只想将所有内容放入一个数组列表 请帮助..再次,我的教授没有教我们面对面,这就是为什么 我真的尽力在youtube上观看视频,也可以在这里提问。
答案 0 :(得分:1)
您的许多代码似乎都假定以相似(如果不相同)的方式与五个Container
对象之一进行交互。首先,您可以使用ArrayList来存储Container对象的列表,而不是手动声明每个容器:
public static ArrayList<Container> containerList = new ArrayList<Container>();
然后您可以使用ArrayList.add(E e)
,结合一个for循环或其他某种构造,使用新容器填充此列表:
for (int i = 1; i <= 5; i++) {
Container container = new Container("Container " + i + ": ");
containerList.add(container);
}
同样,您可以使用ArrayList.get(int index)
(如果知道索引)或ArrayList.indexOf(Object o)
(如果有对特定容器的引用)来访问任何特定的容器。这可以替代或补充您的条件语句。例如,您的(contInput.equals("X"))
语句列表可以替换为:
int index = Integer.parseInt(contInput);
System.out.print("Input the name of Container " + index + ": ");
Container container = containerList.get(index - 1); // arrays start at 0, but your numbering starts at 1
String contImp = reader.nextLine();
container.renameCont(contImp);
container.printContainer();
希望这会有所帮助。
答案 1 :(得分:0)
您可以像这样将容器添加到ArrayList中:
ArrayList<Container> containers = new ArrayList<>();
containers.add(new Container("Container 1: "));
containers.add(new Container("Container 2: "));
containers.add(new Container("Container 3: "));
然后像这样获得它们:
Container firstContainer = containers.get(0);
Container secondContainer = containers.get(1);