只在第一列中打印带有重复值的2D数组的第一个元素

时间:2021-11-13 21:21:34

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

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

如您所见,它在第一列中有一些重复的数字,如10010和20003,我只想打印出第一个弹出的数字。这意味着我想打印出println

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

10002、2010004、72 10008、12 10010、37 10007、28 20003、42端

(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...

(注意:第一列中出现重复数字的第一个元素在第二列总是最大的int数,例如:10010、37>34和20003、42>38。)

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

编辑:这里是包含XLS文件片段的完整代码

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...

这是我正在读取的XLS文件的一个片段。这个程序应该是灵活的,可以在相同格式的excel文件上使用。基本的任务是添加匹配产品ID的单元,然后以行/列格式将它们返回。客户ID是无关紧要的。我不能预先设置数组大小,因为程序必须能够读取不同的excel文件,这些文件可能有不同的行数……

4 个解决方案

#1


0  

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
   }
}

#2


0  

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]

[10002,20][10004,72][10008,12][10010,37][10007,28][2003,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.

说明:使用不允许重复的集合。当我们向集合中添加一个元素时,它将返回布尔值,如果最近添加的元素是唯一的,则返回布尔值。在这种情况下,我们将从数组中打印特定的行。

#3


0  

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.

当printed.add(行[0]);它被称为两种情况之一:值已经在打印中,因此打印的大小不会改变。该值不在打印中,因此添加了元素。

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

之前的check prin. size()>只有在元素之前没有打印时才为真。

#4


0  

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

#1


0  

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
   }
}

#2


0  

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]

[10002,20][10004,72][10008,12][10010,37][10007,28][2003,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.

说明:使用不允许重复的集合。当我们向集合中添加一个元素时,它将返回布尔值,如果最近添加的元素是唯一的,则返回布尔值。在这种情况下,我们将从数组中打印特定的行。

#3


0  

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.

当printed.add(行[0]);它被称为两种情况之一:值已经在打印中,因此打印的大小不会改变。该值不在打印中,因此添加了元素。

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

之前的check prin. size()>只有在元素之前没有打印时才为真。

#4


0  

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