The concept of arrays in Java

Source: Internet
Author: User

The definition of an array:

Dynamic initialization mode;

1. Declare and open an array

String str[]=new string[3]; // 3 Indicates the length of the array

2. Distribution Complete

String str[]=null; str=new string[3];

Static initialization

1. Simplified format

String str [] = {"1", "2", "3"};

2. Full format

New string[]{"1", "2", "3"}

Array of memory parsing, array belonging to reference passing

 Public class test{    publicstaticvoid  main (String args[]) {        int New int [3]; // to open an array        of length 3 Data[0]=1;        data[1]=2;        data[2]=3;

int temp[] = data;
TEMP[0] = 99

for (int x=0;x<data.length;x++) {

            System.out.println (Data[x]);}}    

When the value of temp[0] changes, then data[0] will also be changed, which is the reference pass;

(There are pictures here)

Definition of a two-dimensional array

 Public classtest{ Public Static voidMain (String args[]) {intData[][] =New int[][]{            {1,2,3},            {4,5,6},            {7,8,9}        }; //making an array of output operations//Outer loop control line         for(intx=0; x<data.length;x++){            //Inner loop control column             for(inty=0; y<data[x].length;y++) {System. out. Print (data[x][y]+"\ t"); } System. out. println (); }    }}

One method in array sorting

 Public classtest{ Public Static voidMain (String args[]) {intData[] =New int[]{Ten, -,3, +,8, -, -, -, -, -,2};        print (data);        Sort (data);    print (data); }     Public Static voidSortintarr[]) {         for(intx=0; x<arr.length;x++) {//the number of times the outer loop primarily controls sorting             for(inty=0; y<arr.length-1; y++) {//The inner loop is primarily a sort of array                if(arr[y]>arr[y+1]){                    inttemp=Arr[y]; Arr[y]= arr[y+1]; Arr[y+1]=temp; }                            }        }    }     Public Static voidPrintinttemp[]) {         for(intx=0; x<temp.length;x++) {System. out. Print (temp[x]+","); } System. out. println (); }}

Transpose of an array

//Transpose of an array Public classtest{ Public Static voidMain (String args[]) {intData[] =New int[]{1,2,3,4,5,6,7,8,9,10};        print (data);        reverse (data);    print (data); }     Public Static voidReverseintarr[]) {        intHead=0;//increase from the beginning        intTail=arr.length-1;//descending from the tail         for(intx=0;x<arr.length/2;x++){            intt=Arr[head]; Arr[head]=Arr[tail]; Arr[tail]=T; Tail--; Head++; }    }     Public Static voidPrintinttemp[]) {         for(intx=0;x<temp.length;x++) {System.out.print (temp[x]+ ",");    } System.out.println (); }}

Two-dimensional array transpose

 Public classtest{ Public Static voidMain (String args[]) {intData[][] =New int[][]{            {The},            {4,5,6},            {7,8,9}        };        print (data);        reverse (data);    print (data); }     Public Static voidReverseintarr[][]) {         for(intx=0;x<arr.length;x++){             for(inty=x;y<arr[x].length;y++){                    intt=Arr[x][y]; Arr[x][y]=Arr[y][x]; ARR[Y][X]=T; }        }    }     Public Static voidPrintinttemp[][]) {         for(intx= 0;x<temp.length;x++){             for(inty=0;y<temp[x].length;y++) {System.out.print (Temp[x][y]+ ",");        } System.out.println (); }    }}

Common methods in arrays

Array copy: System.arraycopy

 Public classtestdemo{ Public Static voidMain (String args[]) {intDataa[] =New int[]{1,2,3,4,5,6,7,8,9};//Source Array        intDatab[] =New int[]{11,22,33,44,55,66,77,88,99};//array of copiesSystem.arraycopy (dataa,2,datab,3,2);    Print (Datab); }     Public Static voidPrintinttemp[]) {         for(intx= 0;x<temp.length;x++) {System.out.print (temp[x]+ ",");    } System.out.println (); }}
11, 22, 33, 3, 4, 66, 77, 88, 99,

Array sorting methods: Java.util.Array.sort (array name);

Array of first Contact objects: the so-called object array, which is to save the object of the reference type in the form of an array

Code instance

classbook{PrivateString title; Private DoublePrice ;  PublicBook (String title,DoublePrice ) {         This. title=title;  This. price=Price ; }     PublicString toString () {return"The name of the book:" + This. Title + ", Price:" + This. Price; }} Public classtest{ Public Static voidMain (String args[]) {book book[]=Newbook[]{NewBook ("Java", 11.3),            NewBook ("JavaScript", 17.3),            NewBook ("HTML", 9.3)        };  for(intx=0;x<book.length;x++) {System.out.println (book[x]); }    }}

The concept of arrays in Java

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.