One dimensional array

Java The array of is a reference type ,c The array of languages is in the stack

The element is an array of reference data types, and each element is instantiated .

Declaration method of one dimensional array Type var[];  eg: int a1[];

What array cannot be assigned length ,eg  int a1[5];   //false

java Use keywords in new Create array object , The format is :

【 Array name = new The type of the array element [ Number of array elements ]】

 

Dynamic initialization and default initialization

dynamic initialization : Array definition is done separately from allocating space and assigning values to array elements

Default initialization : Reference type when array , Its elements are equivalent to the member variables of a class , Therefore, each element is also implicitly initialized according to the rules of member variables

 

Q:args[] What is stored ?

public class TestSearch { public static void main(String[] args) { int a[] =
{ 1, 3, 6, 8, 9, 10, 12, 18, 20, 34 }; int i = 12;
System.out.println(binarySearch(a,i)); } public static int binarySearch(int[]
a,int num) { if(a.length == 0) return -1; int startPos = 0; int endPos =
a.length - 1; int m = (startPos + endPos)/2; while(startPos <= endPos) { if(num
== a[m]) return m; if(num > a[m]) { startPos = m+1; } if(num < a[m]) { endPos =
m-1; } m = (startPos + endPos) / 2; } return -1; } } public class Count3Quit2
{ public static void main(String[] args) { KidCircle kc = new KidCircle(500);
int countNum = 0; Kid k = kc.first; while(kc.count > 1) { countNum ++;
if(countNum == 3) { countNum = 0; kc.delete(k); } k = k.right; }
System.out.println(kc.first.id); } } class Kid{ int id; Kid left; Kid right; }
class KidCircle{ int count = 0; Kid first,last; KidCircle(int n){ for(int i=0;
i<n; i++) { add(); } } void add() { Kid k = new Kid(); k.id = count; if(count
<= 0) { first = k; last = k; k.left = k; k.right = k; }else { last.right = k;
k.left = last; k.right = first; first.left = k; last = k; } count ++; } void
delete(Kid k) { if(count <= 0) { return; }else if(count == 1) { first = last =
null; }else { k.left.right = k.right; k.right.left = k.left; if(k == first) {
first = k.right; } else if( k == last) { last = k.left; } } count --; } }
Learning object oriented process

Two dimensional array

A two-dimensional array can be regarded as an array of elements .eg:

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

java The declaration and initialization of multidimensional arrays in should be in the order from high dimension to low dimension ,
int a[][] = new int[3][]; a[0] = new int[2]; a[1] = new int[3]; a[2] = new
int[2]; int t1[][] = new int [][4]; //false

 
import javax.xml.crypto.Data; public class TestDateSort { public static void
main(String[] args) { Date[] days = new Date[5]; days[0] = new Date(2006,5,4);
days[1] = new Date(2006, 7, 4); days[2] = new Date(2008, 5, 4); days[3] = new
Date(2004, 5, 9); days[4] = new Date(2004, 5, 4); Date d = new Date(2006,7,4);
String str = String.valueOf(d); bubbleSort(days); for(int
i=0;i<days.length;i++) { System.out.println(days[i]); }
System.out.println(binarySearch(days,d)); } public static Date[]
bubbleSort(Date[] a) { int len = a.length; for(int i = len-1;i>=1;i--) {
for(int j = 0;j<=i-1;j++) { if(a[j].compare(a[j+1]) > 0) { Date temp = a[j];
a[j]=a[j+1]; a[j+1]=temp; } } } return a; } public static int
binarySearch(Date[] days,Date d) { if (days.length==0) return -1; int startPos
= 0; int endPos = days.length-1; int m = (startPos + endPos) / 2;
while(startPos <= endPos){ if(d.compare(days[m]) == 0) return m;
if(d.compare(days[m]) > 0) { startPos = m + 1; } if(d.compare(days[m]) < 0) {
endPos = m -1; } m = (startPos + endPos) / 2; } return -1; } } class Date { int
year,month,day; Date(int y,int m,int d){ year = y; month = m; day = d; } public
int compare(Date date) { return year > date.year ? 1 :year < date.year? -1
:month > date.month ? 1 : month < date.month ? -1 : day > date.day ? 1 : day <
date.day ? -1 : 0; } public String toString() { return "Year:Month:Day -- " +
year + "-" + month + "-" + day; } }
Copy of array

public static void arraycopy(Object src, int srcPos, Object dest.int
destPos,int length)

Technology