如何改变数组元素的值在二维数组? [英] How to change value of array element in 2D arrays?
本文介绍了如何改变数组元素的值在二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
比方说,我得到这个图,打印出:
00000
00000
00000
我要如何更改元素[0] [0]成一个X?
在换句话说,如何使它看起来像这样使用屏幕输入:
X0000
00000
00000
解决方案
这条命令:
进口的java.util。*;
进口的java.lang。*;
一流的主
{
公共静态无效的主要(字串[] args)抛出java.lang.Exception的
{
串[] []数组= {{0,0,0},{0,0,0},{0,0,0}} ;
的System.out.println(前:);
printArray(阵列);
阵列[0] [0] =×;
的System.out.println(后);
printArray(阵列);
}
私有静态无效printArray(字符串[] []数组){
的for(int i = 0; I< array.length;我++){
对于(INT J = 0; J<数组[0] .length; J ++){
System.out.print(数组[I] [J]);
}
的System.out.println();
}
}
}
或者去这里: http://ideone.com/2DQC1
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
解决方案
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
这篇关于如何改变数组元素的值在二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文