在Java中填充2D数组
我是java新手,我正在为之奋斗!我已经编写了以下代码,但不断出现错误。目前我所要做的就是用字母a填充一个5x5矩阵。这是我到目前为止所拥有的,我不确定是否也需要发布错误?任何帮助都将不胜感激在Java中填充2D数组,java,Java,我是java新手,我正在为之奋斗!我已经编写了以下代码,但不断出现错误。目前我所要做的就是用字母a填充一个5x5矩阵。这是我到目前为止所拥有的,我不确定是否也需要发布错误?任何帮助都将不胜感激 public class Encryption { private String Unencoded, FiveLetterKeyword, EncryptedMessage; //constructor method public Encryption(String U,
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;
}
}
}
Java中的数组是基于零的,这意味着它们从索引零开始,范围一直到index array.length-1
代码从1开始行和列,这意味着您将跳过行/列0的初始化。这可能是至少一些问题的根源,因为您将5x5数组行/列0,1,2,3,4用作4x4数组行/列1,2,3,4
还有一个事实是,加密方法实际上没有对数组进行任何赋值。您可能希望按如下方式对其进行初始化:
// 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;
}
}
}
您的原始代码有几个问题。查看注释中的注释条目以获得单独的解释。Java中的数组是以零为基础的,这意味着它们从索引零开始,范围一直到索引array.length-1
代码从1开始行和列,这意味着您将跳过行/列0的初始化。这可能是至少一些问题的根源,因为您将5x5数组行/列0,1,2,3,4用作4x4数组行/列1,2,3,4
还有一个事实是,加密方法实际上没有对数组进行任何赋值。您可能希望按如下方式对其进行初始化:
// 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;
}
}
}
您的原始代码有几个问题。请查看注释中的注释条目以获得单独的解释。您缺少了您试图完成的最关键的部分 您在哪里设置字母A的矩阵 将加密函数更改为以下内容:
//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;
}
}
}
你错过了你想要完成的最关键的部分 您在哪里设置字母A的矩阵 将加密函数更改为以下内容:
//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;
}
}
}
是的,张贴错误并指出错误发生的确切位置。我还建议谷歌搜索这些错误,以便努力了解它们是什么,为什么会发生,以及如何修复它们。。。并且可以只使用…,当定义字符时,您需要使用“a”而不是a。单引号定义字符,双引号定义字符串,张贴错误并指出错误发生的确切位置。我还建议谷歌搜索这些错误,以便努力了解它们是什么,为什么会发生,以及如何修复它们。。。当定义字符时,你需要使用“a”而不是a。单引号定义字符,双引号定义字符串这很好,非常感谢。虽然我仍然得到4个错误:它是说非静态变量加密不能从静态上下文引用?@missrigby-我猜这是因为你在矩阵方法上使用了static关键字,这意味着它看不到非静态加密字段。消除矩阵前面的静电,它可能会消失…啊!太神了非常感谢!!太好了,非常感谢。虽然我仍然得到4个错误:它是说非静态变量加密不能从静态上下文引用?@missrigby-我猜这是因为你在矩阵方法上使用了static关键字,这意味着它看不到非静态加密字段。消除矩阵前面的静电,它可能会消失…啊!太神了非常感谢!!