Basic Learning tutorials for Java arrays _java

Source: Internet
Author: User
Tags array definition

Java array declaration, creation, initialization
How to declare a one-dimensional array:

Type var[]; or type[] var;

You cannot specify its length (the number of elements in an array) when declaring an array.

Use the keyword New in Java to create an array object in the following format:

Array name = new array element type [number of array elements]

Instance:
Testnew.java:

Program code:

public class Testnew 
{public 
   static void Main (String args[]) { 
     int[] s; 
     int i; 
     s = new int[5]; 
     for (i = 0; i < 5; i++) { 
       s[i] = i; 
     } 
     for (i = 4; I >= 0; i--) { 
       System.out.println ("" + S[i]);}}  
 

Class

1. Dynamic initialization: The array definition is separated from the operation of the Arrays allocation space and assignment;
2. Static initialization: Assigning space and assigning values to array elements while defining numbers;
3. Default initialization: An array is a reference type and its elements are equivalent to a class's member variable, so after the array allocates space, each element is also initialized by the hermit according to the rule of the member variable.
Instance:


Testd.java (Dynamic):

Program code:

public class TESTD 
{public 
   static void Main (String args[]) { 
     int a[]; 
     A = new int[3]; 
     A[0] = 0; 
     A[1] = 1; 
     A[2] = 2; 
     Date days[]; 
     days = new Date[3]; 
     Days[0] = new Date (2008,4,5); 
     DAYS[1] = new Date (2008,2,31); 
     DAYS[2] = new Date (2008,4,4); 
   } 
 
Class Date 
{ 
   int year,month,day; 
   Date (int year, int month, int day) { 
     this.year = year; 
     This.month = month; 
     This.day = day; 
   } 
 

Tests.java (Static):

Program code:

public class TestS   
{public   
   static void Main (String args[]) {   
     int a[] = {0,1,2};   
     Time times [] = {new Time (19,42,42), New Time (1,23,54), New Time (5,3,2)}}   
 
Class time   
{   
   int hour,min,sec;   
   Time (int hour, int min, int sec) {   
     this.hour = hour;   
     this.min = min;   
     This.sec = sec;   
   }   
}  

Testdefault.java (default):

Program code:

public class Testdefault   
{public   
   static void Main (String args[]) {   
     int a [] = new int [5];   
     System.out.println ("" + a[3]);   
   }   
  

Java Basics One dimension array and multidimensional array
In the Java language, arrays are one of the simplest types of composite data. An array is a collection of ordered data, each of which has the same data type, and can uniquely determine the elements in an array with a uniform array name and subscript. The array has a one-dimensional array and a multidimensional array.

1. Definition of one-dimensional array

Type arrayname[]; 

Type can be any type of data in Java, including simple types and compound types.

For example:

int intarray[];  
Date datearray[]; 

2. Initialization of one-dimensional arrays

(1) Static initialization

int intarray[]={1,2,3,4};  
String stringarray[]={"abc", "How" and "You"}; 

(2) Dynamic initialization

1 Arrays of simple types

int intarray[];  
Intarray = new 
int[5]; 

2 array of compound types

String stringarray[];  
String Stringarray = new string[3];/* space for each element in the array (32 bits)/ 
stringarray[0]= new String ("how");//Open space for the first array element 
stringarray[1]= New String ("are");//Open space for second array element 
stringarray[2]= new String ("you");//space for third array element 

3. A reference to a one-dimensional array element

The array elements are referenced in the following ways:

Arrayname[index]
Index is an array subscript, which can be an integer constant or an expression, with the subscript starting at 0. Each array has an attribute length indicating its length, for example: Intarray.length indicates the length of the array intarray.

Multidimensional arrays

In the Java language, a multidimensional array is treated as an array of arrays.

1. Definition of two-dimensional arrays

Type arrayname[] [];  
type [] []arrayname; 

2. Initialization of two-dimensional arrays

(1) Static initialization

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

In the Java language, the array space is not continuously allocated because it is considered an array of arrays, so it is not required to have the same size for each dimension of the two-dimensional array.

(2) Dynamic initialization

1) Allocate space directly for each dimension, the format is as follows:

Arrayname = new Type[arraylength1][arraylength2];  
int a[] [] = new INT[2][3]; 

2 start from the highest dimension and allocate space for each dimension respectively:

Arrayname = new type[arraylength1][];  
Arrayname[0] = new TYPE[ARRAYLENGTH20];  
ARRAYNAME[1] = new TYPE[ARRAYLENGTH21];  
... Arrayname[arraylength1-1] = new TYPE[ARRAYLENGTH2N]; 

3) Example: The dynamic initialization of a two-dimensional simple data type array is as follows,

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

For arrays of two-dimensional composite data types, you must first allocate the reference space for the highest dimension and then the lower-dimensional space allocation in sequence. Also, you must allocate space for each array element individually.

For example:

String s[] [] = new string[2][];  
s[0]= New string[2];//allocates a reference space for the highest dimension 
s[1]= new string[2];//Allocate a reference space for the highest dimension 
s[0][0]= new String ("good"); Allocate space separately for each array element 
s[0][1]= new String ("Luck");//Allocate space for each array element 
s[1][0]= new String ("to");//Allocate space for each array element individually 
s[1][1]= New String ("You");//Allocate space for each array element individually 

3. Reference to two-dimensional array elements

For each element in a two-dimensional array, the reference is: Arrayname[index1][index2]

For example:

NUM[1][0]; 

4. Examples of two-dimensional arrays:

"Example" two matrices multiplied

Public 
class matrixmultiply{  
public 
static 
void Main (String args[]) {  
int i,j,k;  
int a[][]=new 
int [2][3];//////dynamic initialization of a two-dimensional array 
int b[][]={{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}};//static initialization 


A two-dimensional array

int c[][]=new 
int[2][4];//dynamic initialization of a two-dimensional array for 
(i=0;i<2;i++) for  
(j=0; j<3; j)  
a[j]= (i+1) * (j +2);  
for (i=0;i<2;i++) {for  
(j=0;j<4;j++) {  
c[j]=0;  
for (k=0;k<3;k++)  
c[j]+=a[k]*b[k][j];  
}  
System.out.println ("*******matrix c********");/print Matrix C token for 
(i=0;i<2;i++) {for  
(j=0;j<4;j++)  
System.out.println (c[j]+ "");  
System.out.println ();  
}  
  

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.