Java 排序数组列表 公共类Saleitem{ 公共产品产品=空; 公共int numberofproduct=0; 静态ArrayList Saleitemarray=新ArrayList(); 静态ArrayList总计=新ArrayList(); //阅读销售数据 公共静态void salesData(){ 字符串SalesDataCSV=“SalesData.csv”; BufferedReader br=null; 字符串行=”; 字符串cvsSplitBy=“,”; System.out.println(“\n销售数据文件已打开\n”); 试一试{ int currentcustomer=1; int lastcustomer=1; 双和=0; br=新的BufferedReader(新的文件读取器(SalesDataCSV)); line=br.readLine(); System.out.println(“-------------------------------------------------------------”; System.out.println(“销售数据文件”); System.out.println(“客户ID、产品ID、单位数量”); System.out.println(“-------------------------------------------------------------”; 而((line=br.readLine())!=null){ 字符串字段[]=行分割(cvsSplitBy); 如果(字段长度>1){ 字符串currentcustomerID=字段[0]; 字符串currentproductID=字段[1]; 字符串currentunitnumber=字段[2]; Product currentproduct=null; currentcustomer=Integer.parseInt(currentcustomerID); int currentproductid=Integer.parseInt(currentproductid); int currentproductunit=Integer.parseInt(currentunitnumber); //------------------------------------- //开始产品/销售项目处理 //------------------------------------- System.out.println(currentcustomer+,“+currentproductid+,”+currentproductunit); //////////////////// if(lastcustomer==currentcustomer){ Saleitem salesItemObject=新Saleitem(currentproductid,currentproductunit, Product.getUnitPrice(currentproductid)); 添加(salesItemObject); }否则{ //销售收据日期、时间等。 SaleCreaction salesReceiptObject=新的SaleCreaction(lastcustomer,lastcustomer, 求和,“2/20/16”(int)(Math.random()*2000); Salereceipt.receipt.add(saleReceiptObject); lastcustomer=当前客户; Saleitemarray.clear(); 总和=0; } /////////////////////////// //找到客户订购的正确产品 对于(int i=0;i
销售数据从具有客户ID[0]、产品ID[1]、订购单位[2]的文件导入Java 排序数组列表 公共类Saleitem{ 公共产品产品=空; 公共int numberofproduct=0; 静态ArrayList Saleitemarray=新ArrayList(); 静态ArrayList总计=新ArrayList(); //阅读销售数据 公共静态void salesData(){ 字符串SalesDataCSV=“SalesData.csv”; BufferedReader br=null; 字符串行=”; 字符串cvsSplitBy=“,”; System.out.println(“\n销售数据文件已打开\n”); 试一试{ int currentcustomer=1; int lastcustomer=1; 双和=0; br=新的BufferedReader(新的文件读取器(SalesDataCSV)); line=br.readLine(); System.out.println(“-------------------------------------------------------------”; System.out.println(“销售数据文件”); System.out.println(“客户ID、产品ID、单位数量”); System.out.println(“-------------------------------------------------------------”; 而((line=br.readLine())!=null){ 字符串字段[]=行分割(cvsSplitBy); 如果(字段长度>1){ 字符串currentcustomerID=字段[0]; 字符串currentproductID=字段[1]; 字符串currentunitnumber=字段[2]; Product currentproduct=null; currentcustomer=Integer.parseInt(currentcustomerID); int currentproductid=Integer.parseInt(currentproductid); int currentproductunit=Integer.parseInt(currentunitnumber); //------------------------------------- //开始产品/销售项目处理 //------------------------------------- System.out.println(currentcustomer+,“+currentproductid+,”+currentproductunit); //////////////////// if(lastcustomer==currentcustomer){ Saleitem salesItemObject=新Saleitem(currentproductid,currentproductunit, Product.getUnitPrice(currentproductid)); 添加(salesItemObject); }否则{ //销售收据日期、时间等。 SaleCreaction salesReceiptObject=新的SaleCreaction(lastcustomer,lastcustomer, 求和,“2/20/16”(int)(Math.random()*2000); Salereceipt.receipt.add(saleReceiptObject); lastcustomer=当前客户; Saleitemarray.clear(); 总和=0; } /////////////////////////// //找到客户订购的正确产品 对于(int i=0;i,java,sorting,arraylist,Java,Sorting,Arraylist,销售数据从具有客户ID[0]、产品ID[1]、订购单位[2]的文件导入 我想按产品ID按升序对ArrayList总数进行排序。最好的方法是什么。我是java新手,所以我不太懂语法。你可以使用集合#排序,如下所示 为ProductId添加一个getter,就完成了 public class Saleitem { public Product product = null; public int numberofproduct = 0; static ArrayList&l
我想按产品ID按升序对ArrayList总数进行排序。最好的方法是什么。我是java新手,所以我不太懂语法。你可以使用
集合#排序
,如下所示
为ProductId
添加一个getter,就完成了
public class Saleitem {
public Product product = null;
public int numberofproduct = 0;
static ArrayList<Saleitem> Saleitemarray = new ArrayList<Saleitem>();
static ArrayList<Integer[]> total = new ArrayList<Integer[]>();
//read the sales data
public static void salesData() {
String SalesDataCSV = "SalesData.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
System.out.println("\nThe Sales Data file has been opened\n");
try {
int currentcustomer = 1;
int lastcustomer = 1;
double sum = 0;
br = new BufferedReader(new FileReader(SalesDataCSV));
line = br.readLine();
System.out.println("-----------------------------------------------");
System.out.println("Sales Data File");
System.out.println("Customer ID, Product ID, Number of Units");
System.out.println("-----------------------------------------------");
while ((line = br.readLine()) != null) {
String field[] = line.split(cvsSplitBy);
if(field.length>1) {
String currentcustomerID = field[0];
String currentproductID = field[1];
String currentunitnumber = field[2];
Product currentproduct = null;
currentcustomer = Integer.parseInt(currentcustomerID);
int currentproductid = Integer.parseInt(currentproductID);
int currentproductunit = Integer.parseInt(currentunitnumber);
//-------------------------------------
// START OF PRODUCT/SALE ITEM PROCESSING
//-------------------------------------
System.out.println(currentcustomer + " , " + currentproductid + " , " + currentproductunit);
////////////////////
if (lastcustomer == currentcustomer) {
Saleitem salesItemObject = new Saleitem(currentproductid, currentproductunit,
Product.getUnitPrice(currentproductid));
Saleitemarray.add(salesItemObject);
} else {
// sale receipt date, time, etc.
Salereceipt salesReceiptObject = new Salereceipt(lastcustomer, lastcustomer,
sum, "2/20/16", (int) (Math.random() * 2000));
Salereceipt.receipt.add(salesReceiptObject);
lastcustomer = currentcustomer;
Saleitemarray.clear();
sum = 0;
}
///////////////////////////
//Find the correct product that the customer ordered
for (int i = 0; i < Product.productData.size(); i++){
if (((Product.productData).get(i)).productID == currentproductid){
currentproduct = Product.productData.get(i);
}
}
Saleitem salesItemObject = new Saleitem(currentproduct, currentproductunit);
Saleitemarray.add(salesItemObject);
boolean found = false;
//update total
for (int i = 0; i < total.size(); i++){
//total is an array of arrays =)
//in the array, index 0 is the productID
// index 1 is the total sold of that product
//Find the correct product total
if ((total.get(i))[0] == salesItemObject.product.productID){
//if we found it then we will mark found
//so that we can add in the item if it doesnt exist
//in our total array
found = true;
//increment the total number of prodcuts sold
(total.get(i))[1] += salesItemObject.numberofproduct;
}
}
if (found == false){
Integer[] array = new Integer[2];
// index 0 = product id
// index 1 = total number of products sold
array[0] = salesItemObject.product.productID;
array[1] = salesItemObject.numberofproduct;
total.add(array);
}
//-------------------------------------
// END OF PRODUCT/SALE ITEM PROCESSING
//-------------------------------------
//this is done inside of the constructor
if (currentcustomer == lastcustomer){
sum += currentproduct.productPrice * currentproductunit;
}
}
}
Collections.sort(总计,新比较器(){
@凌驾
公共整数比较(销售项目s1、销售项目s2){
返回s1.getProductId()-s2.getProductId();
}
});
Collections.sort,可能需要一个定制的比较器,或者可能是Thank you的副本,我想这会有用的谢谢!这很有效
Collections.sort(total, new Comparator<Saleitem>(){
@Override
public int compare(Saleitem s1, Saleitem s2) {
return s1.getProductId() - s2.getProductId();
}
});
total.sort((item1, item2) -> item1.getProductId() - item2.getProductId());