【问题标题】:Java Change array elements order based on Specific columnsJava根据特定列更改数组元素顺序
【发布时间】:2020-08-06 13:13:35
【问题描述】:

我从一个文件中提取了以下数组:

[ Car , House , Bike , Index0, Motor, Ski , Index1 , Hello ]

我正在尝试以强制特定元素(index0 -> 0 和 index1 -> 1)到特定位置的方式重新排序此数组,如下所示:

[ Index0 , Index1, Bike , Car, Motor, Ski , House, Hello ]

到目前为止,我能够提取信息、读取数组并获得正确的索引,但似乎知道如何继续替换元素:

    static void loadHeaderSettingName() throws IOException {
    System.out.println("Loading the header settings name [Value][ColIndex]");
    FileInputStream excelFile = new FileInputStream(new File(excelFilePath));
    Workbook workbook = new XSSFWorkbook(excelFile);
    Sheet sheet = workbook.getSheetAt(0);
    Row row = sheet.getRow(0);

    for (Cell cell : row) {
        headerArray.add(cell.getStringCellValue().trim());
    }

    String index0="Index0";
    String index1="Index1";

    System.out.println(headerArray.toString());

    for (int i=0; i <headerArray.size(); i++ ) {
        System.out.println(headerArray.get(i) + " " + i);

        if (headerArray.get(i).equals(index0)) {
            System.out.println("hi");
        }
    }
}

【问题讨论】:

    标签: java arrays loops indexing


    【解决方案1】:

    您可能正在循环中寻找类似的东西。但这不是一个通用的解决方案。就像在这种方法中一样,如果您想交换除 0 和 1 之外的其他索引,那么您必须添加另一个 if 条件

        for( int i = 0; i < headerArray.size(); i++ )
        {
            String temp;
            if( headerArray.get( i ).equalsIgnoreCase( index0 ) )
            {
                temp = headerArray.get( i );
                headerArray.set( i, headerArray.get( 0 ) );
                headerArray.set( 0, temp );
            }
            else if( myArr[i].equalsIgnoreCase( index1 ) )
            {
                temp = headerArray.get( i );
                headerArray.set( i, headerArray.get( 1 ) );
                headerArray.set( 1, temp );
            }
        }
        System.out.println ( headerArray );
    

    【讨论】:

    • 感谢@Klaus,我也能够解决,但使用数组而不是 ArrayList,请检查下面的答案并验证列数,因为我使用了 400 多列用于生产文件
    【解决方案2】:

    我能够解决如下

    String index0 = "index0";
        int index0_NEW_POSITION = 0;
    
        String  index1 = "index0";
        int  index1_COLUMN_NEW_POSITION = 1;
    
        int arraySizeBeforeColumnReordering = array.length;
        System.out.println("BEFORE: " + java.util.Arrays.toString(array));
    
        for (int i=0; i <array.length; i++ ) {
    
            if (array[i].equals(index0)) {
                int original_position = i;
                String tmp_original_postion_0 = array[original_position];
                array[original_position]=array[index0_NEW_POSITION];    
                array[index0_NEW_POSITION] = tmp_original_postion_0;
            }
    
            if (array[i].equals(index1)) {
                int original_position = i;
                String tmp_original_postion_0 = array[original_position];
                array[original_position]=array[index1_COLUMN_NEW_POSITION]; 
                array[index1_COLUMN_NEW_POSITION] = tmp_original_postion_0;
            }
    
            }
    
        int arraySizeAfterColumnReordering = array.length;
        System.out.println("AFTER: " + java.util.Arrays.toString(array));
    
        if (arraySizeBeforeColumnReordering == arraySizeAfterColumnReordering){
            System.out.println("The count of column before and after the column ordering is still the same: " + arraySizeBeforeColumnReordering);
        } else {
            System.out.println("The count of column before and after the column ordering is no longer the same");
            System.out.println("The count of column before: " +  arraySizeBeforeColumnReordering);
            System.out.println("The count of column after: " + arraySizeAfterColumnReordering);
        }
    

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多