Dark Horse Programmer _java Array

Source: Internet
Author: User

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

Array
A collection of data of the same type, in fact the array is a container.
The advantage of an array is that it is possible to automatically number the elements in the array starting with 0 to facilitate manipulation of these elements.
Format 1
element type [] Array name =new element type [number of elements or length of array]
Format 2
element type [] Array name =new element type []{1,2,3} This format is static initialization of an array
element type [] array name ={1,2,3}

Memory structure
When a Java program runs, it needs to allocate space in memory. In order to improve the efficiency of computing, space is divided into different regions, because each region has a specific way of processing data and memory management.
Stack memory
For storing local variables, the occupied space is automatically freed when the data is used.
Heap Memory
Arrays and objects, which are stored in heap memory by the instance created by new
Each entity has a memory address value
The variables in the entity have default initialization values
Entities are not being used and will be reclaimed by the garbage collector for an indeterminate period of time
and method area, local method area, register three kinds of total five kinds of memory space.

For a reference type variable, the variable entity is present in the heap, and when the variable is referenced, the memory address of the variable is assigned to the local variable.
for int []arry=new int[3];
The first half is a variable that defines an array type, stored in the stack memory, and the new statement later declares a reference variable that opens up memory in the heap memory, = The memory address in the heap memory is given to the array type in the stack memory for its reference. When two array-type variables point to a reference-type variable, an array-type variable is assigned a value, and the other is re-assigned. A new reference variable is created for each new one.

Common problems in arrays
1 null pointer exception, 2 reference out of range

Common operations in arrays
Traverse
int []arry={1,2,3}
public static void Printarry (Arry []) {
for (int i=0;i<arry.length;i++) {
if (i!=arry.length-1)
System.out.println (arry[i]+ ";");
Else
System.out.println (Arry[i]);
}

}
Get the most value
public static void Getmax (int []arry) {
int max=0;
for (int i=0;i<arry.length;i++) {
if (Arry[i]>arry[max])
Arry[max]=arry[i];
}
return Arry[max];
}
}
Select sort
public static void Selectsort (Int[]arry) {
for (int x=0;x<arry.length-1;x++) {
for (int y=x+1;y<arry.length;y++) {
if (Arry[x]>arry[y]) {
int temp=arry[x];
Arry[x]=arry[y];
Arry[y]=temp;
}
}
}

}
Bubble sort
public static void Selectsort (int[] arry) {
for (int. x=0;x<arry.length-1;x++) {
for (int i=0;i&        lt;arry.length-x-1;i++) {
if (arry[i]>arry[i+1]) {
int temp=arry[y];
ARRY[Y]=ARRY[Y+1];
Arry[y+1]=temp;
}
}
}
}
Extract of the array position substitution function
public static void swap (int []arry,int A, int b) {
int temp=arry[ A];
Arry[a]=arry[b];
Arry[b]=temp;
}
Array lookup
public static int search (int []arry,int key) {
for (int i=0;i<arry.length;i++) {
if (arry[i]=    =key) {
return i;
}
}
return-1;//returns 1
}
for an ordered array binary find
public static int Search (Int[]arry,int a) {
int min=0,m Ax=arry.length-1;
int mid= (arry.length-1)/2;    
while (arry[mid]!=a) {
if (arry[mid]>a) {
max=mid-1;
    }else if (arry[mid]<a) {
min=mid+1;
      }
if (Min>max)
Return-1;
Mid= (max+min)/2;
}
return mid;
}

Decimal-Binary conversion
public static void toBin (int num) {
StringBuffer sb=new stringbuffer ();//A container that stores data;
while (    num>0) {
Sb.append (num%2);
NUM=NUM/2;
}
System.out.println (Sb.reserve ());//reverse-print the data in the container.
}

decimal-"hexadecimal"
public static void Tohex (int num) {
StringBuffer sb=new StringBuffer ();
for (int i=0;i<8;x++) {
int temp=num&15;
if (temp>9)
Sb.append ((char) (temp-10+ ' A '));
Else
Sb.append (temp);
num=num>>>4;
}
System.out.println (Sb.reverse ());
}
Tabular: Temporarily store all the elements and set up correspondence
Each time the value of & is indexed to the established array table, the corresponding element can be found. This is much simpler than doing the math every time.
public static void Tohex (int num) {
Char[]chs={', ', ', ', ', '}
Char[]arr=new char[8];//defines a storage array to store the found elements.
int pos=arry.length;//defines a flag as the pointer to the array element.
while (num!=0) {
int temp=num&15;
ARRY[--POS]=CHS[TEMP];
num=num>>>4;
}
for (int x=pos;x<arry.length;x++) {
System.out.print (arry[x]+ ",");
}

}

Two-dimensional arrays
Rectangular array
int [][]arry=new int[3][2]
Jagged array
int [][]arry=new int[2][]
Arry[0]=new Int[3];
Arry[1]=new Int[4];
Summation of two-dimensional arrays
int sum=0;
for (int x=0;x<arry.length;x++) {
for (int y=0;y<arry[x].length;y++) {
Sum+=arry[x][y];
}
}

Dark Horse Programmer _java Array

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.