Java Learning Fourth Days group

Source: Internet
Author: User

An array is a collection of ordered data, and each element in the array has the same array name and subscript to uniquely determine the elements in the array.

§5.1 One-dimensional arrays

Definition of one or one-D arrays

Type arrayname[];

Where the type can be any data type in Java, including a simple type combination type, the array name arrayname to a valid identifier, [] indicating that the variable is an array type variable. For example:

int intarray[];

An integer array is declared, and each element in the array is an integral type of data. Unlike C, C + +, Java does not allocate memory for array elements in the definition of arrays, so [] does not indicate the number of elements in an array, that is, the length of an array, and any element that is not accessible to an array as defined above. We have to allocate memory space for it, then we use the operator new, which is in the following format:

Arrayname=new Type[arraysize];

Where arraysize indicates the length of the array. Such as:

Intarray=new Int[3];

Allocates a memory space occupied by 3 int integers for an integer array.

Typically, these two parts can be combined in the following format:

Type arrayname=new type[arraysize];

For example:

int intarray=new int[3];

References to two or one-D array elements

Once you have defined an array and allocated memory space for it with the operator new, you can refer to each element in the array. The array elements are referenced in the following way:

Arrayname[index]

Where: Index is an array subscript, which can be an integer constant or an expression. such as A[3],b[i] (i is integral type), c[6*i] and so on. The subscript starts at 0, and the length of the array is reduced by 1. For the In-tarray number in the example above, it has 3 elements, namely:

INTARRAY[0],INTARRAY[1],INTARRAY[2]. Note: no intarray[3].

In addition, unlike C, C + +, Java array elements are cross-checked to ensure security. At the same time, for each array there is a property length that indicates its lengths, for example: Intarray.length indicates the length of the array intarray.

Example 5.1

public class arraytest{
public static void Main (String args[]) {
int i;
int a[]=newint[5];
for (i=0;i<5;i++)
A[i]=i;
for (i=a.length-1;i>=0;i--)
System.out.println ("a[" +i+ "]=" +a[i]);
}
}

The results of the operation are as follows:

C:\>java Arraytest

A[4]=4
A[3]=3
a[2]=2
A[1]=1
A[0]=0

The program assigns a value to each element in the array and then outputs it in reverse order.

Initialization of three or one-D arrays

An array of elements can be assigned according to the example above. You can also initialize an array while it is being defined.

For example:

int a[]={1,2,3,4,5};

Each element of an array is separated by a comma (,), and the system automatically allocates a certain amount of space.

Unlike in C, Java does not require the array to be static (static).

Example of a four or one-D array program:

Example 5.2Fibonacci series

The Fibonacci sequence is defined as:

F1=f2=1,fn=fn-1+fn-2 (n>=3)

Public classfibonacci{
public static void Main (String args[]) {
int i;
int f[]=new int[10];
F[0]=f[1]=1;
for (i=2;i<10;i++)
F[I]=F[I-1]+F[I-2];
for (i=1;i<=10;i++)
System.out.println ("f[" +i+ "]=" +f[i-1]);
}
}

The result of the operation is:

C:\>java Fibonacci

F[1]=1
F[2]=1
f[3]=2
F[4]=3
F[5]=5
F[6]=8
F[7]=13
F[8]=21
F[9]=34
F[10]=55

Example 5.3 Bubble Method ordering (small to large)

The Bubbling method compares the adjacent two elements and puts the small elements in front.

public class bubblesort{
public static void Main (String args[]) {
int i,j;
int intarray[]={30,1,-9,70,25};
int l=intarray.length;
for (i=0;i<l-1;i++)
for (j=i+1;j<l;j++)
if (Intarray[i]>intarray[j]) {
int t=intarray[i];
INTARRAY[I]=INTARRAY[J];
intarray[j]=t;
}
for (i=0;i<l;i++)
System.out.println (intarray[i]+ "");
}
}

The result of the operation is:

C:\>java Bubblesort
-9
1
25
30
70

§ 5.2 Multi-dimensional arrays

Like C, C + +, multidimensional arrays in Java are treated as arrays of arrays. For example, a two-dimensional array is a special one-dimensional array with each element being a one-dimensional array. Here we mainly take the two-dimensional number as an example to illustrate that the high-dimensional situation is similar.

Definition of one or two-D arrays

Two-dimensional arrays are defined in the following ways:

Type arrayname[][];

For example:

int intarray[][];

As with a one-dimensional array, the array element is not allocated memory space, and the operator new is used to allocate memory before each element can be accessed.

For high-dimensional arrays, there are several ways to allocate memory space:

1 allocate space directly for each dimension, such as:

int a[][]=new int[2][3];

2 allocate space for each dimension, starting from the highest dimension, for example:

int a[][]=new int[2][];
A[0]=new Int[3];
A[1]=new Int[3];

Complete the same functionality in 1. This is different from C, C + +, and must indicate the length of each dimension once in C and C + +.

References to two or two-D array elements

For each element in a two-dimensional array, the reference is: Arrayname[index1][index2] where index1, Index2 is subscript, can be integer constant or expression, such as a[2][3], and so on, the subscript of each dimension starts from 0.

Initialization of three or two-D arrays

There are two ways of doing this:

1 assigns a value directly to each element.

2 is initialized at the same time as the array is defined.

such as: int a[][]={{2,3},{1,5},{3,4}};

Defines an array of 3x2 and assigns values to each element.

Examples of four or two-D arrays:

Example 5.4 matrix multiplication

Two matrices Amxn, bnxl multiplied to get cmxl, each element cij=?aik*bk (I=1..M,N=1..N)

public class matrixmultiply{
public static void Main (String args[]) {
int i,j,k;
int a[][]=new int[2][3];
int b[][]={{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}};
int c[][]=new int[2][4];
for (i=0;i<2;i++)
for (j=0;j<3;j++)
a[i][j]= (i+1) * (j+2);
for (i=0;i<2;i++) {
for (j=0;j<4;j++) {
c[i][j]=0;
for (k=0;k<3;k++)
C[I][J]+=A[I][K]*B[K][J];
}
}
System.out.println ("\n***matrixa***");
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
System.out.print (a[i][j]+ "");
System.out.println ();
}
System.out.println ("\n***matrixb***");
for (i=0;i<3;i++) {
for (j=0;j<4;j++)
System.out.print (b[i][j]+ "");
System.out.println ();
}
System.out.println ("\n***matrixc***");
for (i=0;i<2;i++) {
for (j=0;j<4;j++)
System.out.print (c[i][j]+ "");
System.out.println ();
}
}
}

The result is:

C:\>java matrixmultiply

for (j=0;j<4;j++)
System.out.print (c[i][j]+ "");
System.out.println ();
}
}
}

The result is:

C:\>java matrixmultiply

matrixa***
2 3 4
4 6 8
matrixb***
1 5 2 8
5 9 10-3
2 7-5-18
matrixc***
25 65 14-65
50 130 28-130

If you have learned linear algebra, you should be able to better understand multidimensional arrays.
Multidimensional arrays and matrices are tightly coupled.

A[I][J] is the element of column j-1 of Line i-1, since the subscript is starting from 0.
Like what:
An array: 1 2 3
4 5 6
A[0][0]=1 a[0][1]=2 a[0][2]=3
A[1][0]=3 a[1][1]=5 a[1][2]=6
I recommend you to read some books:
1.Thinking in Java
2.Java 2 Core Technology
3.java2 Practical Tutorials
4. Object-oriented programming and the Java language

Java Learning Fourth Days group

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.