The application of array in Java programming

Source: Internet
Author: User

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 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.