简体   繁体   English

用Java填充2D数组

[英]Filling a 2D array in Java

I am new to java and I am struggling immensely! 我是Java的新手,我在努力挣扎! 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? 目前,我要做的只是用字母A填充5x5矩阵。这是到目前为止的内容,我不确定是否也需要发布错误? Any help would be really greatly appreciated. 任何帮助将不胜感激。

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

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). 您的代码rowcolumn起始row1这意味着您跳过了行/列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. 还有一个事实,就是您的Encrypt方法实际上并未对该数组进行任何分配。 You probably want to initialize it like this: 您可能想要像这样初始化它:

// 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中的NOTE条目以获取单独的说明。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM