我正在开发一个简单的POS程序。我有几个循环将零件信息添加到列表(invoiceList),然后在另一个列表(invoiceListMain)下订购所有零件,然后将所有发票订购到主列表(invoiceLogList)。所以基本上我在列表中的列表中有列表。
当我在print语句中输出整个列表时,这是有效的。但我需要能够解压缩列表。
[[738,[113,5.0,43.45],[248,2.0,12.78]],[459,[248,10.0,63.9],[113,1.0,8.69]]
我收到此错误
ArrayListDemo.java:136:错误:不兼容的类型:String不能转换为ArrayList for(ArrayList line:invoiceLogList)
当我这样做时:
for(int i=0; i < invoiceLogList.size(); i++)
{
for(ArrayList<String> line : invoiceLogList)
{
System.out.print(" \n" + line.get(0));
}
}
在负面评级之前,请留下理由。
从研究开始,我认为这与我如何在列表中的列表中编译所有列表有关。
invoiceLogList.add(invoiceListMain.toString());
我必须添加.toString()才能使其正常工作。所以我的猜测是我不能将它作为一个数组解压缩,因为它不再是一个数组?这是一个清单?我不是百分百肯定。
有没有人有更好的方法或如何解压缩数据的建议?
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class ArrayListDemo {
public static void main(String[]args){
//Part 1
ArrayList<String> part1 = new ArrayList<String>();
part1.add("06011");
part1.add("Castrol EDGE 0W20 Motor Oil - 1 qt");
part1.add("9.49");
//Part 2
ArrayList<String> part2 = new ArrayList<String>();
part2.add("248");
part2.add("Castrol EDGE 5W30 Motor Oil - 1 qt");
part2.add("6.39");
//Part 3
ArrayList<String> part3 = new ArrayList<String>();
part3.add("03050");
part3.add("Castrol Hypuron 10W30 Motor Oil - 1 gal");
part3.add("6.39");
//Part 4
ArrayList<String> part4 = new ArrayList<String>();
part4.add("113");
part4.add("Castrol Pyroplex Protection ES Multi-Purpose Grease - 14 oz");
part4.add("8.69");
//Part 5
ArrayList<String> part5 = new ArrayList<String>();
part5.add("020");
part5.add("Castrol GTZ ULTRACLEAN 5W20 Motor Oil - 6 gal");
part5.add("69.99");
//adding parts to main Part List
ArrayList<ArrayList<String>> mainPartList = new ArrayList<ArrayList<String>>();
mainPartList.add(part1);
mainPartList.add(part2);
mainPartList.add(part3);
mainPartList.add(part4);
mainPartList.add(part5);
//-----INVOICE INPUT----------------------------------------------------------------------------
//create a new scanner
Scanner input = new Scanner(System.in);
//Invoice Input
String a = "";
String invoiceNumber = "";
String p = "";
double q = 0.0;
double partPrice = 0.0;
double partSum = 0.0;
String partNum = "";
String partDescrip = "";
String c = "";
//Invoice Array
ArrayList<String> invoiceLogList = new ArrayList<>();//Stores all invoices created
do{//loop to create invoice
ArrayList<String> invoiceListMain = new ArrayList<>();//stores individual invoices
Random generator = new Random();//creates random invoice number
int sequence = generator.nextInt(1000);
invoiceListMain.add(String.format("%03d", sequence));//invoice number
System.out.print("Invoice #: " + sequence);
do{//loop to add parts to invoice
ArrayList<String> invoiceList = new ArrayList<String>();//stores part#, qty, sumTotal
System.out.print("\n\nPart Number: ");
p = input.next();
//fetch part info from array mainPartList
for(ArrayList<String> part : mainPartList)
{
if(part.contains(p))
{
for (int i = 0; i < 1; i++)
partNum = part.get(0);
partDescrip = part.get(1);
invoiceList.add(partNum);//add part# to list
System.out.print("\t" + partNum + " - " + partDescrip);
partPrice = Double.parseDouble(part.get(2));//grabs price of part in list
}
}
System.out.print("\nQuantity: ");
q = input.nextDouble(); //asks quantity sold
invoiceList.add(Double.toString(q));//add quantity to list
partSum = partPrice * q;
invoiceList.add(Double.toString(partSum));//add part sum to list
System.out.printf("\t $%.2f",partSum);
System.out.print("\n\n---Add to cart?: ");
c = input.next();
invoiceListMain.add(invoiceList.toString());//adding part order to mainList
}while(c.equalsIgnoreCase("Y"));
//Create Invoice Loop
System.out.print("\n\nCreate a new Invoice? ");
a = input.next();
invoiceLogList.add(invoiceListMain.toString());
}while(a.equalsIgnoreCase("Y"));
//Print all invoices
for(int i=0; i < invoiceLogList.size(); i++)
{
for(ArrayList<String> line : invoiceLogList)
{
System.out.print(" \n" + line.get(0));
}
}
}
}
答案 0 :(得分:1)
我尝试那个例子,那就是我得到的东西也许就是你想要的东西!
ArrayList<ArrayList<ArrayList<String>>> invoiceLogList= new ArrayList<ArrayList<ArrayList<String>>> ();
ArrayList<ArrayList<String>> invoiceListMain= new ArrayList<ArrayList<String>>();
ArrayList<String> c = new ArrayList<String>();
c.add("13");
c.add("50");
c.add("43.45");
ArrayList<String> d = new ArrayList<String>();
d.add("728");
invoiceListMain.add(d);
invoiceListMain.add(c);
invoiceLogList.add(invoiceListMain);
for(int i=0; i < invoiceLogList.size(); i++)
{
for(ArrayList<ArrayList<String>> line :invoiceLogList)
{
System.out.print(" \n" + line.get(0));
}
}
输出:
[728]
BUILD SUCCESSFUL (total time: 0 seconds)
答案 1 :(得分:0)
这是因为您在两个循环标题中访问相同的列表,我相信进行此更正应该可以解决问题:
for(int i=0; i < invoiceLogList.size(); i++)
{
for(ArrayList<String> line : invoiceLogList.get(i))
{
System.out.print(" \n" + line.get(0));
}
}
答案 2 :(得分:0)
如果你看看你的打印件,
你可以看到738和459是一个太高的水平。
以下是深度分解:
列表有2个元素
第1个包含3个元素的列表:
*一根绳子738
*一个字符串列表[113,5.0,43.45]
*一系列字符串[248,2.0,12.78]
这被认为是一个列表清单(来自你的故事) 这是异常告诉你的。