Java 如何在二维阵列上使用插入排序?
因此,我使这段代码几乎可以工作,但我希望在数组上使用插入排序,并使输出仅显示按插入排序按产品ID排序的结果。每个产品ID应具有相同的相应单元数。这些单位不应单独分类。唯一的订单是按产品ID排序,这是我的基本意思Java 如何在二维阵列上使用插入排序?,java,arrays,csv,Java,Arrays,Csv,因此,我使这段代码几乎可以工作,但我希望在数组上使用插入排序,并使输出仅显示按插入排序按产品ID排序的结果。每个产品ID应具有相同的相应单元数。这些单位不应单独分类。唯一的订单是按产品ID排序,这是我的基本意思 import java.util.*; import java.io.*; class newversion { public static int [][] table; // the output table pub
import java.util.*;
import java.io.*;
class newversion {
public static int [][] table; // the output table
public static int numOfRows; //number of rows used up in the table
public static int lookfor(int productID){
int location = -1; //-1 an error
for(int i = 0; i < numOfRows; i++){
if (table[i][0] == productID){
location = i;
}
}
return location;
}
/*
here is my modified bubble sort code. I based it on this, but done differently:
http://stackoverflow.com/questions/23283655/bubble-sort-on-2d-array-java
public static void swap(int int1, int int2, int[] array) {
if(int1 == int2){
return;
}
else{
int temp = int2;
array[int2] = array[int1];
array[int2] = temp;
}
}
but it didn't work and I had to try something else
*/
public static boolean contains(int productID){
if (lookfor(productID) == -1){
return false;
}
return true;
}
public static void main(String[] args) {
File file = null;
Scanner scanner = null;
try{
file = new File("data.csv");
scanner = new Scanner( file );
}
catch(Exception e){
System.out.println("Error opening file!");
System.exit(1);
}
//citation of idea for sorting method in 2D array: http://stackoverflow.com/questions/23283655/bubble-sort-on-2d-array-java
//I'm using bubble sort on a 2D array
//this is his code
/*
private static void bubblesort(Integer[] array) {
for (int i = 0; i < array.length; i++) {
for(int j = 0; j < array.length - 1; j++) {
if(array[j].compareTo(array[j+1]) > 0) {
swap(j, j+1, array);
}
}
}
}
private static void swap(Integer int1, Integer int2, Integer[] array) {
if(int1 == int2)return;
Integer temp = new Integer(array[int2]);
array[index2] = array[int1];
array[int1] = temp;
}
*/
//here's my idea for bubble sort on a 2D array
/*
for (int i = 0; i < numOfRows; i++){
for(int j = 0; j < numOfRows - 1; j++) {
if(table[j][0].compareTo(array[j+1][0]) > 0) {
swap(j, j+1, table);
}
}
//this didn't work well either
//Now, I have to try another for-loop
*/
//Count the number of lines in the file
int size_of_file = 0;
while (scanner.hasNextLine()){
scanner.nextLine();
size_of_file++;
}
table = new int[size_of_file][2];
//reset scanner
try{
file = new File("data.csv");
scanner = new Scanner( file );
}
catch(Exception e){
System.out.println("Error opening file!");
System.exit(1);
}
//save the title
String titleLine = scanner.nextLine();
System.out.println(titleLine);
//for each line in the file, store and total it.
numOfRows=0;
while (scanner.hasNextLine()){
String ln = scanner.nextLine();
String[] row = ln.split(",");
System.out.println(row[0] + ", " + row[1]);
if (contains(Integer.parseInt(row[0]))){
//This is the location in the table where the product id exists already.
int location = lookfor(Integer.parseInt(row[0]));
//add the units to what we have in the table
table[location][1] += Integer.parseInt(row[1]);
}
else{
table[numOfRows][0]= Integer.parseInt(row[0]);
table[numOfRows][1]= Integer.parseInt(row[1]);
numOfRows++;
}
}
//output
try{
PrintWriter output = new PrintWriter("output.csv");
output.println(titleLine);
for(int i=0;i<numOfRows;i++){
output.println(table[i][0] + "," + table[i][1]);
}
output.close();
}
catch(Exception e){
System.out.println("Error writing file");
}
}
}
我现在得到的输出:
Product ID Units
10002 20
10004 72
10008 12
10010 37
10007 28
20003 42
30019 56
30020 29
10006 36
20005 32
etc.
我很抱歉,如果这个更新应该作为一个不同的问题发布。让我知道,这样我就可以按照社区标准行事。您会注意到,我发布的输出不是按productID排序的。那是我最不想做的事。除此之外,它基本上是有效的。如果有人因为我没有公布答案而投票否决我,我道歉,因为从技术上讲,这将是对最初问题的相同答案。如果此更新应该是另一个线程,请再次通知我,我将进行编辑。此行中[a]中的a是什么?Array[]arr3=新的ArrayList[a][1];a是行数。我对它进行了编辑,现在它是:Array[][]arr=newarraylist[inta][intb];FileReader FileReader=新的FileReader(Sales Data.xls);所以它是一个数组而不是三个。我觉得这样效率更高。除了文件读取之外,你的代码还有很多问题。一个从excel读取文件的示例,希望能有所帮助
Product ID Units
10002 20
10004 72
10008 12
10010 37
10007 28
20003 42
30019 56
30020 29
10006 36
20005 32
etc.