令牌“=”的语法错误,在此令牌之后的表达式。

时间:2021-05-13 22:29:04

I am getting an error message which i do not under stand, my code is as follows:

我收到了一个错误信息,我不支持,我的代码如下:

public void generate2DArray(ArrayList<String> mapArray, int lineNumber)
{
    lineNumber = lineNumber - 2;
    String [] elementSplit = null;
    char TwoDArray [][] = new char[lineNumber][];
    for(int i = 0; i < mapArray.size(); i++){
        elementSplit = (mapArray.get(i)).split("(?!^)");
        for(int j = 0; j < elementSplit.length; j++){
            TwoDArray [i][j] = [i][elementSplit[j]];
        }
        System.out.println(Arrays.toString(elementSplit));
    }
}

The error occurs during:

时发生错误:

TwoDArray [i][j] = [i][elementSplit[j]];

Where i get the following error:

在这里我得到了以下错误:

Syntax error on token "=", Expression expected after this token

Can anyone advise on this?

有人能给点建议吗?

Thanks very much.

非常感谢。

4 个解决方案

#1


0  

you have error because TwoArray[i][j] is a char just one character not string .

您有错误,因为两个数组[i][j]是一个字符,而不是字符串。

And elementSplit[j] is a String, so you must using this form

elementSplit[j]是一个字符串,所以您必须使用这个表单。

TwoDArray [i][j]= elementSplit[j].charAt(i);

#2


0  

Follwing is a sample for your needs.

以下是您需要的样品。

import java.util.ArrayList;
import java.util.Arrays;

public class ALTest {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<String> al = new ArrayList<String>();
        al.add("################");
        al.add("#..............#");
        al.add("#........G.....#");
        al.add("#..............#");
        al.add("################");
        char[][] twoDarray = get2DArray(al);
    }

    private static char[][] get2DArray(ArrayList<String> mapArray) {
        // TODO Auto-generated method stub
        char [] elementSplit = null;
        char twoDarray [][] = new char[mapArray.size()][];
        for(int i = 0; i < mapArray.size(); i++){
            elementSplit = (mapArray.get(i)).toCharArray();
            twoDarray [i] = elementSplit;
            System.out.println(Arrays.toString(elementSplit));
        }
        return twoDarray;
    }
}

#3


0  

Change

改变

TwoDArray [i][j] = [i][elementSplit[j]];

to

TwoDArray [i][j] = elementSplit[j].charAt(0);

#4


0  

The problem your having is that you are putting things into you 2D array wrong.

你的问题是你把东西放入你的二维数组错误。

TwoDArray [i][j] is a single location so can only hold a single number/character etc. You are trying to put the value i and the value of elementSplit[j] in the same location.

两个darray [i][j]是一个单一的位置,所以只能容纳一个数字/字符等。你试图把i和elementSplit[j]的值放在同一个位置。

So your line:

所以你的线:

TwoDArray [i][j] = [i][elementSplit[j]];

is incorrectly implemented.

是不正确的实施。

You can only do:

你只能做的事:

TwoDArray [i][j] = i;

or

TwoDArray [i][j] = elementSplit[j];

Hope this helps.

希望这个有帮助。

#1


0  

you have error because TwoArray[i][j] is a char just one character not string .

您有错误,因为两个数组[i][j]是一个字符,而不是字符串。

And elementSplit[j] is a String, so you must using this form

elementSplit[j]是一个字符串,所以您必须使用这个表单。

TwoDArray [i][j]= elementSplit[j].charAt(i);

#2


0  

Follwing is a sample for your needs.

以下是您需要的样品。

import java.util.ArrayList;
import java.util.Arrays;

public class ALTest {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<String> al = new ArrayList<String>();
        al.add("################");
        al.add("#..............#");
        al.add("#........G.....#");
        al.add("#..............#");
        al.add("################");
        char[][] twoDarray = get2DArray(al);
    }

    private static char[][] get2DArray(ArrayList<String> mapArray) {
        // TODO Auto-generated method stub
        char [] elementSplit = null;
        char twoDarray [][] = new char[mapArray.size()][];
        for(int i = 0; i < mapArray.size(); i++){
            elementSplit = (mapArray.get(i)).toCharArray();
            twoDarray [i] = elementSplit;
            System.out.println(Arrays.toString(elementSplit));
        }
        return twoDarray;
    }
}

#3


0  

Change

改变

TwoDArray [i][j] = [i][elementSplit[j]];

to

TwoDArray [i][j] = elementSplit[j].charAt(0);

#4


0  

The problem your having is that you are putting things into you 2D array wrong.

你的问题是你把东西放入你的二维数组错误。

TwoDArray [i][j] is a single location so can only hold a single number/character etc. You are trying to put the value i and the value of elementSplit[j] in the same location.

两个darray [i][j]是一个单一的位置,所以只能容纳一个数字/字符等。你试图把i和elementSplit[j]的值放在同一个位置。

So your line:

所以你的线:

TwoDArray [i][j] = [i][elementSplit[j]];

is incorrectly implemented.

是不正确的实施。

You can only do:

你只能做的事:

TwoDArray [i][j] = i;

or

TwoDArray [i][j] = elementSplit[j];

Hope this helps.

希望这个有帮助。