How to add strings from one array into another arr

2020-02-15 12:57发布

问题:

I have an array of strings called initStrings and an array called squares. I need to take the strings from initStrings and add them one by one into different rows in the array squares

static String[] initStrings =
        {
         "...../...\\",
         "..\\.......",
         "......./..",
         "..........",
         "........\\.",
         "..........",
         "..........",
         ".....\\../.",
         "..\\....../",
         ".........."
        };

static int [][] squares;

    public static void initialize()
    {
        int [][] squares = new int [10][10];
        for (col = 0; col < 10; col++)
        {
            for (rows = 0; rows < 10; rows ++)
            {
                squares[col][rows] = initStrings();
            }
        }

I was told in class that I need a nested for loop to do this but inside the rows loop I can't figure out what to put. Any help?

回答1:

You cannot put those strings into an integer array. squares should be an array of string. If it were, you could do :

squares[col][rows] = initStrings[(col * squares.length + rows) % initStrings.length];


回答2:

Something like: In the outer loop: String tempStr= InitStrings[rows]; In the inner loop: squares[cols][rows]= tempStr.chatAt(cols);

But why you define 'squares' as int?



回答3:

it's a little bit confusing what you're trying to achieve, but the answer to your question is that you need another loop to run on the initStrings array (which you called as a function. also it's a 1 dimension array so you need to understand what you want to put inside the squares array which is 2 dimensions)

hope this helps



标签: java arrays