1

I am trying to send a 2D Char-Array from my Spring-Controller to React ... it works when i use Arraylist but doesn't work with "char[ ][ ]"

so this works

@GetMapping(value = "/test")
public ArrayList<String> getTest() {

    ArrayList<String> arr = new ArrayList<String>();
    arr.add("Something");
    arr.add("Somehow");
    return arr;

and this Does not work

@SuppressWarnings("checkstyle:WhitespaceAround")
@GetMapping(value = "/newboard")
public char[][] emptyBoard(char[][] playField) {

    for (char y = 0; y < playField.length; y++) {

        for (char x = 0; x < playField[y].length; x++) {

            playField[y][x] = (' ');

        }
    }
        return playField;
}
2
  • uhm what about using List<List< Character >> Commented Jun 21, 2021 at 12:44
  • I'm trying to use a better and more simple approach, because i don't want to change all the methods in my code... since they all uses "playField as char[][]" @AlbertoSinigaglia Commented Jun 21, 2021 at 12:50

2 Answers 2

1

After a good research i found that it doesn't work sending primitive datatype to Frontend... this is as Answer to this question not all the situations maybe, but at least in my case that was the solution.

you have to try the following:

Either try the Refernce types =>

@GetMapping(value = "/test")
public ArrayList<String> getTest() {

ArrayList<String> arr = new ArrayList<String>();
arr.add("Something");
arr.add("Somehow");
return arr;

OR this approach (From type {Class} ) =>

 @GetMapping(value = "/test")
 public MyModel getTest() {


    MyModel myModel = new MyModel();
    myModel.setFirstName("Somthing");
    myModel.setLastName("Somehow");

    GameUpdate u = new GameUpdate();
    Player p1 = new Player();
    p1.setName("1");
    u.setPlayer1(p1);
    return myModel;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can just change the return type, and so "cast" the return object:

@SuppressWarnings("checkstyle:WhitespaceAround")
@GetMapping(value = "/newboard")
public List<List<Character>> emptyBoard(char[][] playField) {

    for (char y = 0; y < playField.length; y++) {

        for (char x = 0; x < playField[y].length; x++) {

            playField[y][x] = (' ');

        }
    }
    return Arrays.asList(playField)
                .stream()
                .map(Arrays::asList)
                .collect(Collectors.toList());
}

4 Comments

Thanks for the answer but => "List<List<Character>>" this aproach looks like the same as (Arraylist<Arraylist<char[]>>)... your solution maybe usefull but i need to change the variable (playField) in all the methods i used, and that will cause a lot of problem in my game Algorithm.
@BasharBoutros no you don't need to change anything, you will need to change only the return statement on the Controller class where you are returning the matrix
i got this error java: cannot find symbol symbol: method asList() location: class java.util.stream.Collectors
@BasharBoutros sorry, just a typo, try now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.