天天看點

簡單java數組程式_java(數組及常用簡單算法 )

數組

數組:數組是存儲同一種資料類型資料的集合容器。

數組的定義格式:

資料類型[]  變量名  =  new  資料類型[長度];

數組的好處:對配置設定到數組對象中每一個資料都配置設定一個編号(索引值、角标、下标),索引值的範圍是從0開始,最大是:長度-1。

局部變量:如果一個變量是在一個方法(函數)的内部聲明的,那麼該變量就是一個局部變量。

成員變量:成員變量就是定義在方法之外,類之内的.

數組中最常見的問題:

1.NullPointerException 空指針異常原因:引用類型變量沒有指向任何對象,而通路了對象的屬性或者是調用了對象的方法。

2.ArrayIndexOutOfBoundsException索引值越界。原因:通路了不存在的索引值。

數組的初始化方式:

動态初始化:

資料類型[] 變量名 = new 資料類型[長度];

靜态初始化:

資料類型[] 變量名 = {元素1,元素2.....};

如果程式一開始你就已經确定了資料,那麼這時候建議使用靜态初始化。如果資料一開始還不太明确,這時候就建議使用動态初始化。

class Demo7 {

public static void main(String[] args) {

//動态初始化

//int[] arr = new int[10];

//靜态初始化

int[] arr = {10,20,30,40,50};

for(int index = 0 ; index

System.out.print(arr[index]+",");

}

}

}

class Demo8 {

public static void main(String[] args)

{

int[] arr = {-12,-14,-5,-26,-4};

int max = getMax(arr);

System.out.println("最大值:"+ max);

}

public static int getMax(int[] arr) {

int max = arr[0]; //用于記錄最大值

for(int i = 1 ; i < arr.length ; i++){

if(arr[i]>max){ //如果發現有元素比max大,那麼max變量就記錄該元素。

max = arr[i];

}

}

return max;

}

}

選擇排序(直接排序):使用一個元素與其他 的元素挨個比較一次,符合條件交換位置。

class Demo9 {

public static void main(String[] args) {

int[] arr = {12,5,17,8,9}; //對于5元素的數組,隻需要找出4個最大值就可以排序了。

selectSort(arr);

}

public static void selectSort(int[] arr) {

//把最大值放在首位置。

for(int j = 0; j

for(int i = j+1 ; i

if(arr[i]>arr[j]){

//交換位置

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

//周遊數組,檢視效果

System.out.print("目前的元素:");

for (int i = 0 ; i

System.out.print(arr[i]+",");

}

}

}

冒泡排序:冒泡排序的思想就是使用相鄰的兩個 元素挨個比較一次,符合條件交換位置。

class Demo10 {

public static void main(String[] args) {

int[] arr = {12,8,17,5,9}; // 最大的索引值: 4 容量:5

bubbleSort(arr);

}

public static void bubbleSort(int[] arr){

// 把最大值放在最後一個位置

for(int j = 0 ; j

for(int i = 0 ; i

//相鄰的元素比較

if(arr[i]>arr[i+1]){

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

}

}

//周遊數組,檢視效果

System.out.print("目前的元素:");

for (int i = 0 ; i

System.out.print(arr[i]+",");

}

}

}

折半查找法(二分法): 使用前提必需是有序的數組。

class Demo12 {

public static void main(String[] args) {

int[] arr = {12,16,19,23,54};

//int index = searchEle(arr,23);

int index = halfSearch(arr,116);

System.out.println("元素所在的索引值是:"+ index);

}

public static int halfSearch(int[] arr, int target){

//定義三個變量分别記錄最大、最小、中間的查找範圍索引值

int max = arr.length-1;

int min = 0;

int mid = (max+min)/2;

while(true){

if(target>arr[mid]){

min = mid+1;

}else if(target

max = mid -1;

}else{

//找到了元素

return mid;

}

//沒有找到的情況

if (max

return -1;

}

//重新計算中間索引值

mid = (min+max)/2;

}

}

public static int searchEle(int[] arr, int target) {

for(int i = 0 ; i

if(arr[i]==target){

return i;

}

}

return -1;

}

}

二維數組: 二維數組就是數組中的數組。

二維數組的定義格式:

資料類型[][] 變量名 = new 資料類型[長度1][長度2];

二維數組 的初始化方式:

動态初始化:

資料類型[][] 變量名 = new 資料類型[長度1][長度2];

靜态初始化:

資料類型[][] 變量名 = {{元素1,元素2...},{元素1,元素2...},{元素1,元素2...} ..}

class Demo16 {

public static void main(String[] args) {

int[][] arr = {{10,11,9},{67,12},{33,35,39,40}};

//周遊二維數組

for(int i = 0; i

for(int j = 0 ; j

System.out.print(arr[i][j]+",");

}

//換行

System.out.println();

}

}

}

數組的特點:

1.數組隻能存儲同一種資料類型的資料。

2.數組是會給存儲到數組中的元素配置設定一個索引值的,索引值從0開始,最大的索引值是length-1;

3.數組一旦初始化,長度固定。

4.數組中的元素與元素之間的記憶體位址是連續的。