java 用Java填充二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34049324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filling a 2D array in Java
提问by missrigby
I am new to java and I am struggling immensely! I've written the following code but keep getting errors. All I am trying to do at the moment is fill a 5x5 matrix with the letter A. Here's what I have so far, I am not sure if I need to post the errors as well? Any help would be really greatly appreciated.
我是 Java 的新手,我正在苦苦挣扎!我已经编写了以下代码,但不断出现错误。我现在要做的就是用字母 A 填充一个 5x5 矩阵。这是我目前所拥有的,我不确定是否还需要发布错误?任何帮助将不胜感激。
public class Encryption {
private String Unencoded, FiveLetterKeyword, EncryptedMessage;
//constructor method
public Encryption(String U, String F, String E)
{
Unencoded = U;
FiveLetterKeyword = F;
EncryptedMessage = E;
}
//create 2D string array 5 by 5
String Encrypt [][] = new String[5][5];
//create string filled with all letters of the alphabet
String String = new String
("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
//method for loop to print out the matrix
public static void matrix()
//for loop to create matrix rows and columns
{
for (int row = 1; row < Encrypt.length; row++)
{
for (int column = 1; column < Encrypt[row].length; column++)
System.out.print(Encrypt[row][column] + " ");
}
}
//filling the array with the letter A
public char Encrypt(char Encrypt[][])
{
//char[] alpha = alphabets.toCharArray;
//declaring variable to fill array with A
char aChar = "A";
for (int row = 1; row < Encrypt.length; row++)
{
for (int column = 1; column < Encrypt.length; column++)
return Encrypt;
}
}
}
回答by DaoWen
Arrays in Java are zero-based, which means they start at index zero, and range until index array.length-1
.
Java 中的数组是从零开始的,这意味着它们从索引 0 开始,范围直到索引array.length-1
。
Your code starts the row
and column
at 1
—which means you're skipping the initialization of row/column 0. That's probably where at least some of the problems are coming from, since you're using your 5x5 array (rows/columns 0,1,2,3,4) as a 4x4 array (rows/columns 1,2,3,4).
您的代码以row
和开头column
,1
这意味着您正在跳过第 0 行/列的初始化。这可能是至少一些问题的来源,因为您使用的是 5x5 数组(行/列 0,1, 2,3,4) 作为 4x4 数组(行/列 1,2,3,4)。
There's also the fact that your Encrypt
method doesn't actually make any assignments to the array. You probably want to initialize it like this:
还有一个事实是,您的Encrypt
方法实际上并未对数组进行任何分配。你可能想像这样初始化它:
// NOTE: changed return type to void -- this is a side-effect-only method!
public void Encrypt(char Encrypt[][])
{
// NOTE: use single-quotes for chars. double-quotes are for strings.
char aChar = 'A';
// NOTE: changed the starting loop values from `1` to `0`
for (int row = 0; row < Encrypt.length; row++)
{
// NOTE: technically Encrypt.length works here since it's a square
// 2D array, but you should really loop until Encrypt[row].length
for (int column = 0; column < Encrypt[row].length; column++)
{
// NOTE: set each entry to the desired char value
Encrypt[row][column] = aChar;
}
}
}
There are several issues with your original code. Look at the NOTE
entries in the comments for individual explanations.
您的原始代码有几个问题。查看NOTE
评论中的条目以获得单独的解释。
回答by hofan41
You are missing the most crucial part of what you are trying to accomplish.
你错过了你想要完成的最关键的部分。
Where are you setting your matrix to the letter A?
您在哪里将矩阵设置为字母 A?
Change your Encrypt function to the following:
将您的加密功能更改为以下内容:
//filling the array with the letter A
public void Encrypt(char arr[][])
{
//char[] alpha = alphabets.toCharArray;
//declaring variable to fill array with A
char aChar = 'A';
for (int row = 0; row < arr.length; row++)
{
for (int column = 0; column < arr[row].length; column++)
{
arr[row][column] = aChar;
}
}
}