78.7k views
5 votes
Provide Java code to illustrate how to create an array, reference an array, and address an element of an array.

User Freman
by
4.9k points

1 Answer

7 votes

Answer:

Hi!

To create an array:

type[] varname OR type varname[]

where:

  • type is the class of the array.
  • var-name is the name of the variable.
  • [] indicates that the instance would be an array.

Examples:

float floatArray[];

boolean[] booleanArray;

String[] stringArray;

Object objectArray[];

To reference an array:

type varname1[];

type varname2[] = varname1;

where:

  • After assign varname1 to varname2, varname2 points to the elements of varname1;

To address an element in an array:

type varname[n];

type i = varname[i];

If you want to access to the i element of the array you can do : varname[i];

User Cfl
by
6.4k points