Order
Arrays are very important data structures, composed of the same type of data structure is static entities, there are linked lists, queues, stacks, numbers and other data structures, Java also proposed class vector class array. These are all part of the Java data structure, as we have learned the C language version of the data structures, Java data structures are to describe the data structure is just the description of the language is the same.
1. The most important thing in the array is the array subscript, the array subscript and array name are used to give visitors access to the array, the data subscript starting from 0, C[0], is a first data the first element is c[i-1], the array name rule is the same as the variable, and its access format is simple.
Example: C.lenth is the length of an array.
C[a+b]+=2 is the number of A+b value +2, this array is also confusing place, that is, the 7th element and array element 7 is two different concepts, beginners must distinguish between the difference.
2. Space allocation: Any data to occupy space, the array is no exception, Java in the new to give a novel array allocation space.
Cases:
int c[ ]=new int[12];
The format is equivalent to
int c[];
c=new int[12];
Their initialization value is 0.
An array can declare multiple arrays at the same time:
Cases:
string b[ ]=new String[100],x[ ]=new String[27];
Arrays can declare any data type, double string ...
For an example to analyze:
// Fig. 7.5: InitArray.java
// initialize array n to the even integers from 2 to 20
import javax.swing.*;
public class InitArray {
public static void main( String args[] )
{
final int ARRAY_SIZE = 10;
int n[]; // reference to int array
String output = "";
n = new int[ ARRAY_SIZE ]; // allocate array
// Set the values in the array
for ( int i = 0; i < n.length; i++ )
n[ i ] = 2 + 2 * i;
output += "Subscript\tValue\n";
for ( int i = 0; i < n.length; i++ )
output += i + "\t" + n[ i ] + "\n";
JTextArea outputArea = new JTextArea( 11, 10 );
outputArea.setText( output );
JOptionPane.showMessageDialog( null, outputArea,
"Initializing to Even Numbers from 2 to 20",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}