简体   繁体   中英

Only printing the first element of a 2D array with repeating values in first column

I have a 2D array that looks like this.

10002,20
10004,72
10008,12
10010,37
10010,34
10007,28
20003,42
20003,38
10002,16

As you can see, it has some repeating numbers in the first column such as 10010 and 20003, and I only want to print out the first one of each that pops up. So that means I want the println to print out

10002,20
10004,72
10008,12
10010,37
10007,28
20003,42
end

(KEY NOTE:The first element that pops up with repeating numbers in the first column will always have the largest int in the second column, EX: 10010,37>34 and 20003,42>38 ALWAYS.) but I'm not sure how to do so...

Edit: Here is the full code with a snippet of the XLS file

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Read {

            public static void readXLSFile() throws IOException{
        InputStream ExcelFile = new FileInputStream("C:/Sales Data.xls");
        HSSFWorkbook wb = new HSSFWorkbook(ExcelFile);
                HSSFSheet sheet=wb.getSheetAt(0);

                int numRows = sheet.getPhysicalNumberOfRows(); 
                int[][] idSale = new int[numRows][2];

                for(int i=1;i<numRows;i++){
                    HSSFCell proId = sheet.getRow(i).getCell(1);
                    HSSFCell sales = sheet.getRow(i).getCell(2);
                    idSale[i][0]=(int)proId.getNumericCellValue();
                    idSale[i][1]=(int)sales.getNumericCellValue();
                }
                for(int j=1;j<numRows;j++){
                    for(int jj=j+1;jj<numRows;jj++)
                        if(idSale[j][0]==idSale[jj][0]){
                           idSale[j][1]+=idSale[jj][1];
//the problem with this loop is that there are repeated numbers in 
//the first column as I'm comparing the entire array to a copy of itself
//and I'm not sure how to avoid it...
                        }
                }
            }



            public static void main(String[] args) throws IOException {
        readXLSFile();
            }
}

This is a snippet of the XLS file I'm reading off of. http://postimg.org/image/drbq7fucz/ This program is meant to be flexible and be used on excel files with the same format. Basically the assignment is to add the units of matching product ID's and then spit them back out afterwards in a row/column format. Customer ID is irrelevant. I can't preset array size because the program must be able to read different excel files that may have different numbers of rows...

Store a List/Set of values that you have printed.

//provided int[][] rows;
Set<Integer> printed = new HashSet<>();
for(int[] row: rows){
    int before = printed.size();
    printed.add(row[0]);
    if(printed.size()>before){
        System.out.println(Arrays.toString(row));
    }
}

This goes through all of the rows in the array. The Set printed contains all of the first value of the rows that have been printed. before is set to the size of printed, before the new element has been added.

When printed.add(row[0]); is called one of two things happen: The value is already in the printed, so the size of printed doesn't change. The value is not in printed so the element is added.

The check printed.size()>before will be true only if the element has not been printed before.

This is one way to do it. A Set cannot have repeating elements.

Set<Integer> printed = new HashSet<>();

for (int[] item : list) {
   if(!printed.contains(item[0]) {
      printed.add(item[0]);
      // Print your line
   }
}

The following sample program will do the work:

public static void main(String[] args) {
        int[][] sample = {{10002, 20},
                {10004, 72},
                {10008,12},
                {10010,37},
                {10010,34},
                {10007,28},
                {20003,42},
                {20003,38}
        };

        Set<Integer> unique = new HashSet<>();
        boolean newAddition = false;
        for(int[] row : sample) {
             newAddition = unique.add(row[0]);
             if(newAddition) {
                 System.out.println(Arrays.toString(row));
             }
        }
    }

The output is:

[10002, 20] [10004, 72] [10008, 12] [10010, 37] [10007, 28] [20003, 42]

Explanation: It makes use of Set which does not allow duplicates. When we add an element to the set it will return boolean which is true if recently added element is unique. In that case we will print that particular row from array.

Assuming the values are grouped like shown in the example, here's the simplest way of doing it:

int[][] array = { {10002,20},
                  {10004,72},
                  {10008,12},
                  {10010,37},
                  {10010,34},
                  {10007,28},
                  {20003,42},
                  {20003,38} };
int prev = array[0][0] - 1; // make sure it's different from first value
for (int[] pair : array) {
    if (pair[0] != prev) {
        System.out.printf("%d,%d%n", pair[0], pair[1]);
        prev = pair[0];
    }
}

Output

10002,20
10004,72
10008,12
10010,37
10007,28
20003,42

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM