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

£可爱£侵袭症+ 提交于 2019-12-04 02:56:08

问题


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 to make it look like this using Screen input:

X0000
00000
00000

回答1:


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

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




回答2:


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




回答3:


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

     }
}


来源:https://stackoverflow.com/questions/12530380/how-to-change-value-of-array-element-in-2d-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!