首页 > 解决方案 > setting the values of arrays in java by using their indexes

问题描述

How do you initialized an array in java first, and then set values to them by using their indexes? So for example, you make an array in java, and then you want the value of the number 75 index of the array to be set to "seventy five", can you do something like array[75] = "seventy five"?;

String[] array;
array[0] = "zero";
array[1] = "one";
array[2] = "two";

When I tried the codes below it says unknown class array. What am I doing wrong?

String[] array = new String[10];
array[0] = "zero";

标签: javaarraysandroid-studioindexing

解决方案


First, you'll need to point the array reference to an actual array object.

For example,

String[] array = new String[3];

You can initialize the contents like you're doing.

Or you can initialize them in the array creation expression:

String[] array = new String[] { "zero", "one", "two" };

You can also the array initializer by itself in the declaration:

String[] array = { "zero", "one", "two" };

推荐阅读