You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改ArrayList<String[]>中指定行列位置的自定义数字?

Solution to Modify a Specific Row and Column in Your ArrayList<String[]>

Got it, let's walk through how to implement the modification feature you need. Here's a complete, working example with validation and clear steps:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ListModifier {
    public static void main(String[] args) {
        // Initialize your ArrayList with sample data (match your actual setup)
        List<String[]> list = new ArrayList<>();
        list.add("1111 2222 33333".split("\\s+"));
        list.add("4444444 42345234 32424".split("\\s+"));
        list.add("23 453435 3452345 2435234".split("\\s+"));

        Scanner scanner = new Scanner(System.in);
        int[] indices = new int[2];

        // Get row index from user
        System.out.print("Enter a line for the number: ");
        indices[0] = scanner.nextInt();

        // Get column index from user
        System.out.print("Enter a column index for the number: ");
        indices[1] = scanner.nextInt();

        // Validate row index to avoid out-of-bounds errors
        if (indices[0] < 0 || indices[0] >= list.size()) {
            System.out.println("Error: Invalid row index. Valid rows are 0 to " + (list.size() - 1));
            scanner.close();
            return;
        }

        // Retrieve the target row array from the list
        String[] targetRow = list.get(indices[0]);

        // Validate column index for the selected row
        if (indices[1] < 0 || indices[1] >= targetRow.length) {
            System.out.println("Error: Invalid column index. Valid columns for this row are 0 to " + (targetRow.length - 1));
            scanner.close();
            return;
        }

        // Get the custom number to replace with
        System.out.print("Enter the custom number to replace with: ");
        String newNumber = scanner.next(); // Keep as String to match your existing data type

        // Modify the specific column value
        targetRow[indices[1]] = newNumber;

        // Optional: Print updated list to verify changes
        System.out.println("\nUpdated list:");
        for (String[] row : list) {
            System.out.println(String.join(" ", row));
        }

        scanner.close();
    }
}

Key Details to Note:

  • Input Validation: We first check if the row and column indices are within valid bounds to prevent IndexOutOfBoundsException—critical for handling user mistakes.
  • Direct Array Modification: Since ArrayList stores references to String[] objects, modifying targetRow[indices[1]] automatically updates the value in the original list. No need to re-add the array back to the list.
  • Data Type Consistency: Your list uses String elements, so we read the new number as a String to maintain consistency. If you need integer operations later, you can parse it to int and re-convert when storing.

Example Workflow:

If you input:

Enter a line for the number: 0
Enter a column index for the number: 2
Enter the custom number to replace with: 99999

The first row will update from 1111 2222 33333 to 1111 2222 99999.

Edge Cases Handled:

  • Negative row/column indices
  • Row indices larger than the number of rows in the list
  • Column indices larger than the number of columns in the selected row

内容的提问来源于stack exchange,提问作者Subnom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:33:34