gpt4 book ai didi

java - 使用 ListIterator 打印数组列表

转载 作者:行者123 更新时间:2023-12-01 06:54:09 24 4
gpt4 key购买 nike

我正在尝试使用 ListIterator 从 ArrayList 进行打印,我很确定我做错了,因为它不起作用,但我不知道如何修复它。所有这些获取零件号的线都不起作用,不知道为什么;P。任何帮助总是值得赞赏:)。

package invoice;
import static java.lang.System.out;

import java.util.*;

public class InvoiceTest {


public static void print(){

}

public static void main (String args[]) {

Scanner imput = new Scanner (System.in);
ArrayList lInvoice = new ArrayList() ;
int counter = 0;
int partCounter;

out.println("Welcome to invoice storer 1.0!");
out.println("To start please enter the number of items: ");
partCounter = imput.nextInt();


while (counter < partCounter){
counter++;
out.println("Please enter the part number:");
Invoice invoice1 = new Invoice(); //Makes invoice 1 use the invoice class
String partNumber = imput.nextLine();// sets part number to the next imput
//invoice1.setPartNumber(partNumber);// Sets it to the private variable in invoice.java
lInvoice.add(partNumber);

out.println("Please enter in a discription of the part: ");
String partDis = imput.nextLine();
//invoice1.setPartDis(partDis);
lInvoice.add(partDis);

out.println ("Please enter the number of items purchased: ");
int quanity = imput.nextInt();
//invoice1.setQuanity(quanity);
lInvoice.add(quanity);

out.println ("Please enter the price of the item:");
double price = imput.nextDouble();
//invoice1.setPrice(price);
lInvoice.add(price);

}

ListIterator<String> ltr = lInvoice.listIterator();
while(ltr.hasNext());
out.println(ltr.next());
}
}

最佳答案

您的程序中还存在一些其他错误。

首先,您应该向 ArrayList 添加一个类型。由于您尝试添加 int , doubleString ,我建议您创建一个 ArrayList<Object> lInvoice = new ArrayList<Object>() ;

然后只需使用迭代器循环即可:

ListIterator<Object> ltr = lInvoice.listIterator();
while(ltr.hasNext()){
out.println(ltr.next());
}

关于java - 使用 ListIterator 打印数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16701681/

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