How to change value of array element in 2D arrays?

后端 未结 3 931
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 05:54

Let\'s say I got this map that prints out:

00000
00000
00000

How do I change the element in [0][0] into an X?

In other words, how t

相关标签:
3条回答
  • 2021-01-17 06:12

    Considering Its an 2D Array of String Type...

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

    0 讨论(0)
  • 2021-01-17 06:21

    Run this:

    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("");         
                    }
            }
    }
    

    Or go here: http://ideone.com/2DQC1

    0 讨论(0)
  • 2021-01-17 06:30

    In This example user k check array element value and change them

    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();
            }
    
         }
    }
    
    0 讨论(0)
提交回复
热议问题