java基礎--數組以及常用操作

package com.zemeng;


import java.util.Arrays;

import java.util.Date;


public class ArrayDemo {


public static void main( String[] argv ){

/*

 *  java數組:

 * 

 * 一 概述:

 * 1.數組是同一種類型數據的集合,數組就是一個容器,用來裝載其他一系列元素;

 * 

 * 2.數組是一種對象,數組變量是引用類型;數組是分配在堆內存中的;且數組中的每個元素都相當於該對象的成員變量;

 * 數組中的元素會被自動初始化,如果是基礎類型數據,會被初始化爲0;

 * 如果是引用類型數據,會被初始化爲null;

 * 數組中存儲的是對象的引用類型,而不是對象;且默認的初始化是爲null,而不是給你創建一個對象並讓該元素指向;

 * 

 * 3.數組可以存儲任意類型的變量,但是一旦定義之後,能存儲的數據類型也就隨之確定;

 * 

 * 4.數組自動給數組中的元素從0開始編號,方便操作這些元素.

 * 

 * 5.特點:

 * (1) 數組一旦定義,數組的長度就被確定,且無法改變;

 * (2) 數組的元素可以通過下標獲取,並且可以下標形式,對數組元素的值進行修改;

 * 

 * 


   二 數組對象的創建 (new):

 * 方式1:

 * 元素類型[] 數組名 = new 元素類型[數組長度];

 * 

 * 方式2:( 常量形式聲明,數組長度由元素的個數自動確定 )

 * 元素類型[] 數組名 = { 元素1, 元素2, 元素3 }

 */

  /* 代碼: 數組的創建 */

int[] a1 = new int[5];

int[] a2 = {1,2,3,4,5};

 /* 代碼: 數組的遍歷 */

for( int i=0; i<a1.length; i++ ){

System.out.println( a1[i] );

System.out.println( a2[i] );

}

 /* 數組元素的獲取或者賦值 */

a1[0] = 20;

a1[1] = 10;

int value0 = a1[0];

int value1 = a1[1];


/*  數組常見異常:

 * 1.下標越界:

 * 因爲數組的下標從0開始,所以數組中元素的index範圍爲 0~length-1;

 * 

 *  2.空指針異常:

 *  數組是引用類型,有可能指向的是null;

 * */

/* 數組的常見使用

 * 

 *  1. 求數組中的最大最小值

 *  2. 直接排序

 *  3. 冒泡排序

 *  4. 折半查找(二分法)

 *  5. 數組翻轉

 *  */

/* 求最大最小值 */

int max;

int min;

int[] arrInt = {1,-3,5,6,7,33,33,2,54,6,4,3};

if( arrInt == null || arrInt.length==0  ){

return;

}

max = min = arrInt[0];

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

if( max < arrInt[i] ){

max = arrInt[i];

}

if( min > arrInt[i] ){

min = arrInt[i];

}

}

System.out.println( "max:"+max+"  min:"+min );

/* 直接排序 */

for( int i=0; i<arrInt.length-1; i++ ){ /* 第一個元素自然有序,所以只要排序n-1次 */

int temp = arrInt[i];

for( int j=i+1; j<arrInt.length; j++ ){ /* n個元素比較n-1次 */

if( temp > arrInt[j] ){

arrInt[i] = arrInt[j];

arrInt[j] = temp;

temp = arrInt[i];

}

}

}

for( int i=0; i< arrInt.length; i++ ){

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

}

/* 冒泡排序 */

for( int i=0; i<arrInt.length-1; i++ ){

for( int j=1; j<arrInt.length; j++ ){

if( arrInt[j-1] < arrInt[j] ){

arrInt[j-1] = arrInt[j] + arrInt[j-1];

arrInt[j]   = arrInt[j-1] - arrInt[j];

arrInt[j-1] = arrInt[j-1] - arrInt[j];

}

}

}

System.out.println("\n\n\n");

for( int i=0; i< arrInt.length; i++ ){

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

}

System.out.println( "\n\n\n" );

/* 數組二分法查找 */

int first = 0;

int last = arrInt.length;

int value = 3;

while ( first <= last ){  /* 如果是等於,那麼表示是最後一個元素,最後一次查找 */

int middle = (first + last)/ 2;

System.out.println( "middle: "+middle+"   value:"+arrInt[middle] );

if( arrInt[middle] > value ){ /* value在 first~middle範圍內 */

first = middle-1;

}else if( arrInt[middle] < value ){ /* value在middle~last範圍內 */

last = middle+1;

}

else {

System.out.println( "查找到了: "+arrInt[middle] );

break;

}

}

/* 數組翻轉 */

int f = 0;

int l  = arrInt.length-1;

while( f<l ){

arrInt[f] += arrInt[l];

arrInt[l] =  arrInt[f] - arrInt[l];

arrInt[f] = arrInt[f]  - arrInt[l];

f++;

l--;

}

System.out.println("\n\n\n");

for( int i=0; i< arrInt.length; i++ ){

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

}

/* Arrays的使用

*  遍歷: toString()   將數組的元素以字符串的形式返回;

*  排序: sort()       將數組按照升序排列;

*  查找: binarySearch()  在制定數組中查找指定元素,返回元素的索引,如果沒有找到返回(返回-1);

*     使用查找的功能的時候,數組一定要先排序;

*  */

System.out.println("\n\n");

String s =  Arrays.toString( arrInt );

arrInt[0] = 20;

Arrays.sort(arrInt);

System.out.println( s );

System.out.println( Arrays.toString(arrInt) );

  }

}

發佈了70 篇原創文章 · 獲贊 12 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章