Dark Horse Programmer--java Array Learning Summary

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Www.itheima.com

Learn the array, learn how to declare, how to traverse, how to get the most value, and the exchange, the sort method is almost the same, because later all use the collection more.

One-dimensional arrays
1) int[] A; Declaration, not initialized


2) int[] a=new int[5]; Initialize to default value, int type is 0


3) int[] a={1,2,3,4,5}; Initialize to the given value


4) int[] A=new int[]{1,2,3,4,5}; Same (3)
Int[] A=new int[5]{1,2,3,4,5}; Error, cannot define a dimension expression if an array initialization operation is provided


5) int[] A;
A=new Int[5]; Right, same as (2)

Int[] A;
a={1,2,3,4,5}; Error, array constants can only be used in initialization operations, such as (3)


6) int a[];
A[0]=1; Error because the array is not initialized and cannot be assigned a value
a[1]=2;

Two-dimensional arrays
1) int[][] A; Declaration, not initialized


2) int[][] a=new int[2][3]; Initialize to default value, int type is 0


3) int[][] a={{1,2},{2,3},{3,4}}; Initialize to the given value
Int[][] a={{1,2},{2,3},{3,4,5}}; No mistake, the array space is not contiguous, so the size of each dimension is not required


4) int[][] A=new int[2][];
A[0]=new Int[3]; A[0] is actually an array
A[1]=new Int[4]; The size of each dimension can be different;


5) int[][] A=new int[][]{{1,2},{2,3},{3,4,5}}; Same (3)
Int[] A=new int[5]{{1,2},{2,3},{3,4,5}}; Error, cannot define a dimension expression if an array initialization operation is provided

Int[][] A=new int[2][];
a[0]={1,2,3,4,5}; Error, array constants can only be used in initialization operations


6) int[][] A=new int[2][];
A[0][1]=1; Error, second dimension not initialized, cannot assign value, Java.lang.NullPointerException exception


Summary: 1. Two dimension is an array of arrays, the size of the array does not require the same
2. Either one-dimensional or two-dimensional, before use (assignment, access) must be initialized, can be initialized by default with new, or can be initialized with array constants

1. Dynamic initialization: The array definition is performed separately from the assignment of the space and assignment to a set of values;
2. Static initialization: Allocates space and assigns values to the array elements while defining the numbers;
3. Default initialization: An array is a reference type and its elements are equivalent to the member variables of the class, so after the array allocates space, each element is also initialized by the hermit according to the rules 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]);
}
}

Dark Horse Programmer--java Array Learning Summary

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.