我正在尝试使用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());
}
}
Run Code Online (Sandbox Code Playgroud)
您的程序中还有一些其他错误.
首先,你要为你的类型添加一个类型ArrayList.既然你想添加int,double并且String,我建议你创建一个ArrayList<Object> lInvoice = new ArrayList<Object>() ;
然后用你的迭代器循环:
ListIterator<Object> ltr = lInvoice.listIterator();
while(ltr.hasNext()){
out.println(ltr.next());
}
Run Code Online (Sandbox Code Playgroud)