JAVA |仅对2D数组的非空值进行分组

时间:2015-08-20 09:29:15

标签: java arrays

在JAVA中,我有一个二维数组如下:

sl_no    plan_names     Column3    Column4     Column5    Column6
1        abc            product1            
2        stu                
3        vwx                
4        yza            product2            
5        bcd                
6        efg                
7        nop                
8        qrs            product3               product4    product5
9        tuv                

现在,想要在一个单独的数组中列出只有非空值的产品,如下所示:

sl_no    plan_names     products
1        abc            product1
2        yza            poduct2
3        qrs            product3
4        qrs            product4
5        qrs            product5

如果有人指导我如何实现这一目标,对我来说真的很有帮助。

谢谢你, 普利文

2 个答案:

答案 0 :(得分:0)

您可以使用面向对象的方法,将各种数据列存储到对象中,并创建一个对象数组,而不是使用2D数组来存储各种类型的数据。

<强>输出:

--------------------------------------------------------------
s_no      plan_names     Column3        Column4        Column5        
1         abc            product1       null           null           
2         stu            null           null           null           
3         vwx            null           null           null           
4         yza            product2       null           null           
5         bcd            product3       null           product4       

--------------------------------------------------------------
s_no      plan_names     Column3        Column4        Column5        
1         abc            product1       null           null           
2         yza            product2       null           null           
3         bcd            product3       null           product4       
4         bcd            product3       null           product4 

创建一个存储Plan

的类
class Plan
{
    private String planName;
    private String[] columns;

    public Plan(String planName){
        this.planName = planName;
        columns = new String[3];
    }

    public boolean allColumnIsNull(){
        for(String s : columns)
            if(s != null)
                return false;
        return true;
    }

    public void setColumn(String val, int colNum){
        if(colNum >= 0 && colNum < columns.length)
            columns[colNum] = val;
    }

    public String[] getColumns(){
        return columns;
    }

    public String toString(){
        return  (String.format("%-15s", planName) + String.format("%-15s", columns[0]) + 
                 String.format("%-15s", columns[1]) + String.format("%-15s", columns[2]));  
    }
}

使用上面的类,现在可以轻松添加/删除/读取数据。一种创建虚拟数据集的方法:

public static Plan[] createDummyData(){
    String[] names = {"abc", "stu", "vwx", "yza", "bcd"};
    Plan[] p = new Plan[5];

    for(int x=0; x<p.length; x++)
        p[x] = new Plan(names[x]);

    p[0].setColumn("product1", 0);
    p[3].setColumn("product2", 0);
    p[4].setColumn("product3", 0);
    p[4].setColumn("product4", 2);      
    return p;       
}

现在,在main方法中,只需调用方法:

public static void main(String[] args)
{
    Plan[] p = createDummyData();

    //Display All Data
    displayAllData(p);

    //Display non-null data
    ArrayList<Plan> list = getNonEmptyPlans(p);
    Plan[] p2 = list.toArray(new Plan[list.size()]);        
    displayAllData(p2);                                             
}

最后一些方法来做打印数据收集

public static void displayAllData(Plan[] plans){
    int s_no = 1;
    System.out.println("\n----------------------------------------------------");
    System.out.println(String.format("%-10s", "s_no") + String.format("%-15s", "plan_names") + String.format("%-15s", "Column3") + 
                        String.format("%-15s", "Column4") + String.format("%-15s", "Column5"));
    for(Plan p : plans){
        System.out.print(String.format("%-10s", s_no++));
        System.out.println(p);
    }

}

检索非空数据的方法:

public static ArrayList<Plan> getNonEmptyPlans(Plan[] plans){
    ArrayList<Plan> list = new ArrayList<Plan>();
    for(Plan p : plans)
        if(!p.allColumnIsNull())
            for(String s : p.getColumns())
                if(s != null)
                    list.add(p);
    return list;                    
}

答案 1 :(得分:0)

试试这个

    // Original Array which stores all data
    String arr_original[][] = new String[9][6];

    // Array where you have to copy
    String arr2[][] = new String[4][3];

    // Separate index for second array as you don't want null values
    // This index will increment only when product != null
    int row2 = 0;

    // To store Product Name of each row of original Array
    String product = "";

    // Iterate through all the rows of original array
    for (int row = 0; row <= arr_original.length; row++) {

        // Now we need to assign the entry in second array only when product
        // is not null

        // Check if products != NULL
        if (arr_original[row][2] != null 
                || arr_original[row][3] != null
                || arr_original[row][4] != null
                || arr_original[row][5] != null) {

            // Assign first 2 columns to second table
            arr2[row2][0] = arr_original[row][0];
            arr2[row2][1] = arr_original[row][1];

            // Find the product from the other 4 columns
            for (int col = 2; col <= 5; col++) {
                if (arr_original[row][col] != null) {
                    // if product != null, assign this product
                    product = arr_original[row][col];

                    // Since NOT null product is found, break the loop
                    break;
                }
            }
            // Assign product to 3rd column
            arr2[row2][2] = product;
            row2++;
        }
    }

相应地更改数组的大小。