【问题标题】:How to change value of array element in 2D arrays?如何更改二维数组中数组元素的值?
【发布时间】:2012-09-21 12:12:27
【问题描述】:

假设我得到了这张打印出来的地图:

00000
00000
00000

如何将[0][0]中的元素变成X?

换句话说,如何使用屏幕输入使其看起来像这样:

X0000
00000
00000

【问题讨论】:

  • -1 用于基本问题。如果您知道数组是什么,那么只需打开每本 Java 书籍或教程,甚至在谷歌上搜索它,答案就会立即出现。我不喜欢说 RTFM,但在这种情况下,这是你应该做的。

标签: java arrays 2d


【解决方案1】:

考虑到它是 2D ArrayString 类型...

arr[0][0] = "X";

【讨论】:

    【解决方案2】:

    运行这个:

    import java.util.*;
    import java.lang.*;
    
    class Main
    {
            public static void main (String[] args) throws java.lang.Exception
            {
                    String[][] array = {{"0","0","0"},{"0","0","0"},{"0","0","0"}};
    
                    System.out.println("Before: ");
                    printArray(array);
    
    
                    array[0][0] = "x";
                    System.out.println("After: ");
                    printArray(array);
    
            }
    
            private static void printArray(String[][] array){
                    for(int i=0; i<array.length; i++){
                            for(int j=0; j<array[0].length; j++){
                                    System.out.print(array[i][j]);
                            }
                            System.out.println("");         
                    }
            }
    }
    

    或者去这里:http://ideone.com/2DQC1

    【讨论】:

    • 谢谢!但是,如果我想使用屏幕输入编辑 [0][0] 中的元素怎么办?
    【解决方案3】:

    在本例中,用户 k 检查数组元素值并更改它们

    import java.util.Scanner;
    
    public class HelloWorld{
    
         public static void main(String []args){
             Scanner in = new Scanner(System.in);
            int inputcol = 0;
            int inputrow = 0;
            int newnum = 0;
            int uinput = 0;
            int repeat = 1;
            int[][] a = new int[][]{
             { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 },
             { 110, 120, 130, 140, 150, 160, 170, 180, 190, 200 },
             { 210, 220, 230, 240, 250, 260, 270, 280, 290, 300 },
             { 310, 320, 330, 340, 350, 360, 370, 380, 390, 400 },
             { 410, 420, 430, 0, 440, 450, 460, 470, 490, 500 }
            }; 
            while(repeat!=0)
            {
            System.out.println("Select Option:\n 1 for View Value:\n 2 for Replace Value: ");
            uinput = in.nextInt();
            //int b[][]={{1,3,4},{3,4,5}}; 
            if(uinput==1)
            {
            System.out.println("Enter Row: ");
            inputrow = in.nextInt();
            System.out.println("Enter Cols:");
            inputcol = in.nextInt();
            System.out.println(a[inputrow][inputcol]);
            }
            else
            if(uinput==2)
            {
                System.out.println("Enter Row: ");
            inputrow = in.nextInt();
            System.out.println("Enter Cols:");
            inputcol = in.nextInt();
            System.out.println("Enter New Number: ");
            newnum = in.nextInt();
            a[inputrow][inputcol] = newnum;
            }
            else
            {
                System.out.println("Check your input. ");
            }
            System.out.println("Want to repeat it? if yes press 1\n for exit press 0 ");
            repeat = in.nextInt();
            }
    
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多