-3

this is my first question. I have problem with defining arrays in android studio. this is piece of my code:

public class MainActivity extends AppCompatActivity {

private ActivityMainBinding binding;

//The code from input.
private String code;
//size of interpreting memory.
public static final int memorySize=3000;
//Memory of system for interpreting.
private byte memory[3000];

it's compiler's error:

Main.java:43: error: ']' expected
private int blockStack[blockStack_size];
                       ^
Main.java:43: error: illegal start of type
    private int blockStack[blockStack_size];
                                      ^
Main.java:43: error: <identifier> expected
    private int blockStack[blockStack_size];
                                       ^
Main.java:43: error: ';' expected
    private int blockStack[blockStack_size];
                                        ^

i also changed "private" to "private static", "public static", "public" and also removed that identifier, but always the same error.

takendarkk
  • 2,949
  • 7
  • 22
  • 35

1 Answers1

1

Looks like you've got some confusion on how to declare and initialize arrays in Java. A quick overview:

  • To declare an array of Type, you write Type[].
  • When initializing an array, you write new Type[size] (where size is the number of elements the array can hold.

So it looks like you probably want to write

private byte[] memory = new byte[3000];

Documentation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Ben P.
  • 44,716
  • 5
  • 70
  • 96