gpt4 book ai didi

java - 使用循环生成ArrayList

转载 作者:行者123 更新时间:2023-12-02 06:53:11 25 4
gpt4 key购买 nike

在下面的代码中,我尝试将名称、成本和优先级捕获到对象中。我使用 for 循环创建 7 个对象,并将每个对象添加到名为 itemData 的 ArrayList 中。

我认为我的代码不会在每次循环运行时将名称、成本和优先级保留到项目中。非常感谢任何建议。

import java.util.*;


public class Main {

public static List<ItemData> itemData = new ArrayList<ItemData>();
public static void main(String[] args) {
int i=0;
//String name1;
//int priority1;
//double cost1;

String[] item = new String[7];

for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();

ItemData grocItem = new ItemData(name, cost, priority);
itemData.add(grocItem); // add grocery items to itemData ArrayList

};

System.out.println(item.name)

// System.out.println(+ grocItem);
}
//How do I add grocItem to an Array list of other grocItems (6 grocItems from user input array item)
//Main.itemData.add(groclist);


}


Itemdata class:
public class ItemData{
public ItemData(String name, double cost, int priority){

//Main.groclist.add(grocItem);

最佳答案

你的 list 没问题。只是您试图以错误的方式打印列表中的项目。你应该做类似的事情,在 for 循环之后说 -

for(ItemData someItem : itemData){
System.out.println("\nName: " + someItem.name + "\tCost: " + someItem.cost + "\tPriority: " + someItem.priority);
}

您可以从长远来看改进您的代码。

编辑:

如果您愿意学习,这里是您正在尝试做的事情的更好版本-

让我们定义 ItemData(将 ItemData 保存在下面的 ItemData.java 文件中)-

public class ItemData {

private String name;
private double cost;
private int priority;

public ItemData(String name, double cost, int priority){
this.name = name;
this.cost = cost;
this.priority = priority;
}

public void setName(String name){
this.name = name;
}

public void setCost(double cost){
this.cost = cost;
}

public void setPriority(int priority){
this.priority = priority;
}

public String getName(){
return this.name;
}

public double getCost(){
return this.cost;
}

public int getPriority(){
return this.priority;
}
}

现在我们有了 ItemData,我们可以像这样使用它(将下面的 GroceryProgram 保存在与 ItemData.java 相同的目录中名为 GroceryProgram.java 的文件中)-

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class GroceryProgram {

private final static int GROC_SIZE = 6;
private final List<ItemData> itemData = new ArrayList<ItemData>();

private void setUpList(){

Scanner keyboard = new Scanner(System.in);

for (int i = 0; i < GROC_SIZE; i++) {

System.out.print("\nEnter item name (" + i + ") : ");
String name = keyboard.next();

System.out.print("\nEnter the price of item (" + i + ") : ");
double cost = keyboard.nextDouble();

System.out.print("\nEnter Priority Number (" + i + ") : ");
int priority = keyboard.nextInt();

ItemData grocItem = new ItemData(name, cost, priority);
itemData.add(grocItem); // add grocery items to itemData ArrayList
}

keyboard.close();
}

private void displayListItems(){

for(ItemData someItem : itemData){
System.out.println("\nName: " + someItem.getName() + "\tCost: " + someItem.getCost() + "\tPriority: " + someItem.getPriority());
}

}

public static void main(String[] args) {

GroceryProgram groProgram = new GroceryProgram();
groProgram.setUpList();
groProgram.displayListItems();

}

}

要编译,请执行-

javac GroceryProgram.java

执行-

java GroceryProgram

没有问题吗?

关于java - 使用循环生成ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17757989/

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