Initializing Arrays
Individual elements of the array are referenced by the array name and by an integer which represents their position in the array. The numbers we use to identify them are called subscripts or indexes into the array. Subscripts are consecutive integers beginning with 0. Thus the array k above has elements k[0] , k[1] , and k[2] . Since we started counting at zero there is no k[3] , and trying to access it will generate an ArrayIndexOutOfBoundsException .

You can use array elements wherever you'd use a similarly typed variable that wasn't part of an array.

Here's how we'd store values in the arrays we've been working with:

k[0] = 2; k[1] = 5; k[2] = -2; yt[6] = 7.5f; names[4] Fred; This step is called initializing the array or, more precisely, initializing the elements of the array. Sometimes the phrase "initializing the arraywould be reserved for when we initialize all the elements of the array.

For even medium sized arrays, it's unwieldy to specify each element individually. It is often helpful to use for loops to initialize the array. For instance here is a loop that fills an array with the squares of the numbers from 0 to 100.

float[] squares = new float[101]; for (int i=0; i <= 100; i++) { squares[i] = i*i; } Two things you should note about this code fragment:

  • Watch the fenceposts! Since array subscripts begin at zero we need 101 elements if we want to include the square of 100.
  • Although i is an int it becomes a float when it is stored in squares, since we've declared squares to be an array of floats.
One way to avoid fencepost errors is to use the array's built-in length member. This tells you the number of components in the array. In the example above, squares.length equals 101. Thus the loop could have been written like this:

float[] squares = new float[101]; for (int i=0, i < squares.length; i++) { squares[i] = i*i; } Note that the <= changed to a < to make this work.

Shortcuts
It may seem to be a lot of work to set up arrays, particularly if you're used to a more array friendly language like Fortran. Fortunately Java has several shorthands for declaring, dimensioning and strong values in arrays.

We can declare and allocate an array at the same time like this:

int[] k = new int[3]; float[] yt = new float[7]; String[] names = new String[50]; We can even declare, allocate, and initialize an array at the same time providing a list of the initial values inside brackets like so:

int[] k = {1, 2, 3}; float[] yt = {0.0f, 1.2f, 3.4f, -9.87f, 65.4f, 0.0f, 567.9f};

 

 

 

Bhopal news
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india