Java基礎(chǔ)學(xué)習(xí)筆記之?dāng)?shù)組詳解
本文實(shí)例講述了Java基礎(chǔ)學(xué)習(xí)筆記之?dāng)?shù)組。分享給大家供大家參考,具體如下:
數(shù)組的定義于使用
1:數(shù)組的基本概念
一組相關(guān)變量的集合;在Java里面將數(shù)組定義為引用數(shù)據(jù)類型,所以數(shù)組的使用一定要牽扯到內(nèi)存分配;想到了用new 關(guān)鍵字來(lái)處理。
2:數(shù)組的定義格式
區(qū)別:
動(dòng)態(tài)初始化后數(shù)組中的每一個(gè)元素的內(nèi)容都是其對(duì)應(yīng)數(shù)據(jù)類型的默認(rèn)值,隨后可以通過(guò)下標(biāo)進(jìn)行數(shù)組內(nèi)容的修改;
如果希望數(shù)組定義的時(shí)候就可以提供內(nèi)容,則采用靜態(tài)初始化的方式;
a:數(shù)組的動(dòng)態(tài)初始化(聲明并初始化數(shù)組):
數(shù)據(jù)類型 數(shù)組名稱 【】 = new 數(shù)據(jù)類型 【長(zhǎng)度】;
數(shù)據(jù)類型 【】 數(shù)組名稱 = new 數(shù)據(jù)類型 【長(zhǎng)度】
b:數(shù)組的靜態(tài)初始化(在數(shù)組定義的時(shí)候就為其設(shè)置好了里面的內(nèi)容)
簡(jiǎn)化格式:數(shù)據(jù)類型 數(shù)組 數(shù)組名稱 【】 = {數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,。。。};
完整格式:數(shù)據(jù)類型 數(shù)組名稱【】 = new 數(shù)據(jù)類型【】{ 數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,。。。};
3:數(shù)組的特點(diǎn)
通過(guò)腳標(biāo)訪問(wèn):0~n-1;
進(jìn)行數(shù)組操作的時(shí)候往往會(huì)利用for循環(huán)來(lái)完成;
數(shù)組的長(zhǎng)度:“數(shù)組名稱.length”;
4:數(shù)組的引用傳遞
數(shù)組定義時(shí)用到了new,所以這里存在有內(nèi)存關(guān)系匹配。
public class ArrayDemo {
public static void main(String args[]) {
// 使用數(shù)組的靜態(tài)初始化
int data [] = new int [3] ;
data [0] = 10 ; // 為數(shù)組設(shè)置內(nèi)容
data [1] = 20 ; // 為數(shù)組設(shè)置內(nèi)容
data [2] = 30 ; // 為數(shù)組設(shè)置內(nèi)容
for (int x = 0 ; x < data.length ; x ++) {
System.out.println(data[x]) ;
}
}
}

一個(gè)堆內(nèi)存可以被多個(gè)棧內(nèi)存所指向
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {10,20,30} ; // 靜態(tài)初始化
int temp [] = data ; // 引用傳遞
temp [0] = 99 ;
for (int x = 0 ; x < data.length ; x ++) {
System.out.println(data[x]) ;
}
}
}

由于數(shù)組是引用數(shù)據(jù)類型,所以一定要為其開(kāi)辟堆內(nèi)存空間(也就是實(shí)例化對(duì)象)才可以使用,如果使用了未開(kāi)辟堆內(nèi)存空間的數(shù)組則一定會(huì)出現(xiàn)“NullPointerException”異常
5foreach迭代輸出
JDK1.5之后為了減輕下標(biāo)對(duì)程序的影響(以為下標(biāo)處理不當(dāng)則會(huì)出現(xiàn)數(shù)組越界異常),參考了.NET的設(shè)計(jì),引入了增強(qiáng)型for循環(huán):foreach
for(數(shù)據(jù)類型 變量 :數(shù)組名) { }
特點(diǎn):可以自動(dòng)將數(shù)組中的每一個(gè)元素的內(nèi)容取出保存到變量里,這樣就可以直接通過(guò)變量獲取數(shù)組的內(nèi)容,避免了數(shù)組越界。
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {1,2,3,4,5} ;
for (int temp : data) { // 自動(dòng)循環(huán),將data數(shù)組每一個(gè)內(nèi)容交給temp
System.out.println(temp) ;
}
}
}
6:二維數(shù)組
定義格式:
a:動(dòng)態(tài)初始化:數(shù)據(jù)類型 數(shù)組名稱【】【】 = new 數(shù)據(jù)類型【行個(gè)數(shù)】【列個(gè)數(shù)】;
b:靜態(tài)初始化:數(shù)據(jù)類型 【】【】 new 數(shù)據(jù)類型 【】【】 { { 數(shù)據(jù),數(shù)據(jù) …},{ 數(shù)據(jù),數(shù)據(jù)…},… }
public class ArrayDemo {
public static void main(String args[]) {
int data [][] = new int [][] {
{1,2,3,4,5} , {1,2,3} , {5,6,7,8}} ;
for (int x = 0 ; x < data.length ; x ++) {
for (int y = 0 ; y < data[x].length ; y ++) {
System.out.println("data["+x+"]["+y+"] = " + data[x][y]) ;
}
System.out.println() ; // 換行
}
}
}
//輸出結(jié)果:
data[0][0] = 1
data[0][1] = 2
data[0][2] = 3
data[0][3] = 4
data[0][4] = 5
data[1][0] = 1
data[1][1] = 2
data[1][2] = 3
data[2][0] = 5
data[2][1] = 6
data[2][2] = 7
data[2][3] = 8
同一個(gè)二維數(shù)組通過(guò)foreach輸出
public class ArrayDemo {
public static void main(String args[]) {
int data [][] = new int [][] {
{1,2,3,4,5} , {1,2,3} , {5,6,7,8}} ;
for (int temp [] : data) {
for (int num : temp) {
System.out.print(num + "、") ;
}
System.out.println() ;
}
}
}
//輸出結(jié)果
1、2、3、4、5、
1、2、3、
5、6、7、8
7數(shù)組與方法
對(duì)于引用數(shù)據(jù)類型,主要的特點(diǎn)是可以于方法進(jìn)行引用傳遞,數(shù)組本身也是引用數(shù)據(jù)類型,所以自然也可以通過(guò)方法實(shí)現(xiàn)引用傳遞的操作。
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {1,2,3,4,5} ;
printArray(data) ; // 傳遞數(shù)組
}
// 要求接收一個(gè)int型的數(shù)組
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.println(temp[x]) ;
}
}
}
//通過(guò)方法來(lái)接收一個(gè)數(shù)組

public class ArrayDemo {
public static void main(String args[]) {
int data [] = initArray() ; // 通過(guò)方法可以獲得數(shù)組內(nèi)容
printArray(data) ; // 傳遞數(shù)組
}
public static int [] initArray() {
int arr [] = new int [] {1,2,3,4,5} ;
return arr ; // 返回一個(gè)數(shù)組
}
// 要求接收一個(gè)int型的數(shù)組
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.println(temp[x]) ;
}
}
}
//通過(guò)方法返回一個(gè)數(shù)組對(duì)象

public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {1,2,3,4,5} ;
changeArray(data) ; // 修改數(shù)組內(nèi)容
printArray(data) ; // 傳遞數(shù)組
}
public static void changeArray(int arr[]) {
for (int x = 0 ; x < arr.length ; x ++) {
arr[x] *= 2 ; // 每個(gè)元素的內(nèi)容乘2保存
}
}
// 要求接收一個(gè)int型的數(shù)組
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.println(temp[x]) ;
}
}
}
//通過(guò)方法修改數(shù)組內(nèi)容。

下面寫(xiě)一個(gè)案例,總結(jié)上面內(nèi)容
案例;定義一個(gè)int數(shù)組,要求計(jì)算出這個(gè)數(shù)組元素的總和,最大值,最小值,平均值。
//此程序的基本實(shí)現(xiàn)
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {1,2,3,4,5} ;
int sum = 0 ; ;
double avg = 0.0 ;
int max = data[0] ; // 假設(shè)第一個(gè)是最大值
int min = data[0] ; // 假設(shè)第一個(gè)是最小值
for (int x = 0 ; x < data.length ; x ++) {
if (data[x] > max) { // max地位改變了
max = data[x] ;
}
if (data[x] < min) {
min = data[x] ;
}
sum += data[x] ;
}
avg = sum / data.length ;
System.out.println("數(shù)組內(nèi)容總和:" + sum) ;
System.out.println("數(shù)組內(nèi)容平均值:" + avg) ;
System.out.println("數(shù)組內(nèi)容最大值:" + max) ;
System.out.println("數(shù)組內(nèi)容最小值:" + min) ;
}
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.println(temp[x]) ;
}
}
}
問(wèn)題:主函數(shù)所在的類往往被稱為主類,那么既然是主類中不希望涉及過(guò)多復(fù)雜的功能;在開(kāi)發(fā)的過(guò)程中,主方法本身就相當(dāng)于是一個(gè)客戶端,而對(duì)于客戶端的代碼盡量簡(jiǎn)單一些,所以這個(gè)時(shí)候是將一系列的計(jì)算過(guò)程交給單獨(dú)的程序類去完成。
//改善操作設(shè)計(jì)
class ArrayUtil { // 是一個(gè)操作工具的類
private int sum ; // 保存總和
private double avg ; // 保存平均值
private int max ; // 保存最大值
private int min ; // 保存最小值
public ArrayUtil(int data[]) { // 進(jìn)行數(shù)組計(jì)算
this.max = data[0] ; // 假設(shè)第一個(gè)是最大值
this.min = data[0] ; // 假設(shè)第一個(gè)是最小值
for (int x = 0 ; x < data.length ; x ++) {
if (data[x] > max) { // max地位改變了
this.max = data[x] ;
}
if (data[x] < min) {
this.min = data[x] ;
}
this.sum += data[x] ;
}
this.avg = this.sum / data.length ;
}
public int getSum() {
return this.sum ;
}
public double getAvg() {
return this.avg ;
}
public int getMax() {
return this.max ;
}
public int getMin() {
return this.min ;
}
}
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {1,2,3,4,5} ;
ArrayUtil util = new ArrayUtil(data) ; // 數(shù)據(jù)計(jì)算
System.out.println("數(shù)組內(nèi)容總和:" + util.getSum()) ;
System.out.println("數(shù)組內(nèi)容平均值:" + util.getAvg()) ;
System.out.println("數(shù)組內(nèi)容最大值:" + util.getMax()) ;
System.out.println("數(shù)組內(nèi)容最小值:" + util.getMin()) ;
}
}
8:數(shù)組操作案例:數(shù)組反轉(zhuǎn)
做法一:定義一個(gè)新的數(shù)組而后按照逆序的方式保存(會(huì)產(chǎn)生無(wú)用的垃圾空間)
class ArrayUtil {
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.print(temp[x] + "、") ;
}
System.out.println() ;
}
}
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {1,2,3,4,5,6,7,8,9} ;
int temp [] = new int [data.length] ; // 第二個(gè)數(shù)組
int foot = temp.length - 1; // 第二個(gè)數(shù)組的腳標(biāo)
for (int x = 0 ; x < data.length ; x ++) {
temp[foot --] = data[x] ;
}
data = temp ;
ArrayUtil.printArray(data) ;
}
}


做法二:在一個(gè)數(shù)組上進(jìn)行轉(zhuǎn)置
class ArrayUtil {
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.print(temp[x] + "、") ;
}
System.out.println() ;
}
}
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {1,2,3,4,5,6,7,8,9} ;
int center = data.length / 2 ; // 確定轉(zhuǎn)換的次數(shù)
int head = 0 ; // 操作腳標(biāo)
int tail = data.length - 1 ; // 操作腳標(biāo)
for (int x = 0 ; x < center ; x ++) {
int temp = data [head] ;
data [head] = data [tail] ;
data [tail] = temp ;
head ++ ;
tail -- ;
}
ArrayUtil.printArray(data) ;
}
}
比較兩種方式:第一種循環(huán)次數(shù)較多,會(huì)產(chǎn)生垃圾;第二種實(shí)現(xiàn)循環(huán)次數(shù)較低,但是存在if判斷增加了
時(shí)間復(fù)雜度,可是減少了無(wú)用對(duì)象的產(chǎn)生,提升了性能。
//將轉(zhuǎn)換功能變?yōu)轭惗x
class ArrayUtil {
public static void reverse(int data[]) {
int center = data.length / 2 ; // 確定轉(zhuǎn)換的次數(shù)
int head = 0 ; // 操作腳標(biāo)
int tail = data.length - 1 ; // 操作腳標(biāo)
for (int x = 0 ; x < center ; x ++) {
int temp = data [head] ;
data [head] = data [tail] ;
data [tail] = temp ;
head ++ ;
tail -- ;
}
}
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.print(temp[x] + "、") ;
}
System.out.println() ;
}
}
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {1,2,3,4,5,6,7,8,9} ;
ArrayUtil.reverse(data) ; // 轉(zhuǎn)置處理
ArrayUtil.printArray(data) ;
}
}
9數(shù)組相關(guān)類操作方法
Java語(yǔ)言本身提供有數(shù)組的相關(guān)支持處理。
a:數(shù)組排序:Java.util.Arrays.sort(數(shù)組名稱)
class ArrayUtil {
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.print(temp[x] + "、") ;
}
System.out.println() ;
}
}
public class ArrayDemo {
public static void main(String args[]) {
int data [] = new int [] {23,12,1,234,2,6,12,34,56} ;
java.util.Arrays.sort(data) ; // 排序
ArrayUtil.printArray(data) ;
}
}
b:數(shù)組拷貝:System.arraycopy(源數(shù)組,源數(shù)組開(kāi)始點(diǎn),目標(biāo)數(shù)組,目標(biāo)數(shù)組開(kāi)始點(diǎn),拷貝長(zhǎng)度)
class ArrayUtil {
public static void printArray(int temp []) {
for (int x = 0 ; x < temp.length ; x ++) {
System.out.print(temp[x] + "、") ;
}
System.out.println() ;
}
}
public class ArrayDemo {
public static void main(String args[]) {
int dataA [] = new int [] {1,2,3,4,5,6,7,8,9} ;
int dataB [] = new int [] {11,22,33,44,55,66,77,88,99} ;
System.arraycopy(dataA,5,dataB,3,3) ;
ArrayUtil.printArray(dataB) ;
}
}
//結(jié)果:
11、22、33、6、7、8、77、88、99、
10方法可變參數(shù)
需求:定義一個(gè)方法,可以實(shí)現(xiàn)任意多個(gè)整型數(shù)據(jù)的相加處理。
傳統(tǒng)實(shí)現(xiàn)。
class ArrayUtil {
public static int sum(int [] data) {
int sum = 0 ;
for (int temp : data) {
sum += temp ;
}
return sum ;
}
}
public class ArrayDemo {
public static void main(String args[]) {
System.out.println(ArrayUtil.sum(new int [] {1,2,3})) ;
}
}
上述代碼可以實(shí)現(xiàn)任意多個(gè)數(shù)字的參數(shù)內(nèi)容傳遞,但是與實(shí)際的要求并不符合,實(shí)際要求的是可以傳遞任意多個(gè)參數(shù),而不是一個(gè)數(shù)組。
從JDK1.5開(kāi)始為了方便開(kāi)發(fā)組進(jìn)行可變參數(shù)的定義。
class ArrayUtil {
public static int sum(int ... data) { // 變種數(shù)組
int sum = 0 ;
for (int temp : data) {
sum += temp ;
}
return sum ;
}
}
public class ArrayDemo {
public static void main(String args[]) {
System.out.println(ArrayUtil.sum(1,2,3,4)) ;
System.out.println(ArrayUtil.sum(new int [] {1,2,3})) ;
}
}
//輸出
10
6
總結(jié):可變參數(shù)的最大作用:在以后進(jìn)行一些程序類設(shè)計(jì)或者開(kāi)發(fā)者調(diào)用的時(shí)候,利用次種形式可以避免數(shù)組的傳遞操作;可變參數(shù)的本質(zhì):依然屬于數(shù)組。
11對(duì)象數(shù)組(重點(diǎn))
之前接觸到的都是基本數(shù)據(jù)類型定義的數(shù)組;Java程序中各種數(shù)據(jù)類型都可以成為數(shù)組類型,所以類也可以成為數(shù)組類型:對(duì)象數(shù)組。
動(dòng)態(tài)初始化: 類 對(duì)象數(shù)組名稱 【】 = new 類 【長(zhǎng)度】;
靜態(tài)初始化: 類 對(duì)象數(shù)組名稱 【】 = new 類 【】{實(shí)例化對(duì)象,實(shí)例化對(duì)象,… };
動(dòng)態(tài)初始化每個(gè)元素為null;
//動(dòng)態(tài)初始化
class Person {
private String name ;
private int age ;
public Person(String name,int age) {
this.name = name ;
this.age = age ;
}
public String getInfo() {
return "姓名:" + this.name + "、年齡:" + this.age ;
}
// setter、getter略
}
public class ArrayDemo {
public static void main(String args[]) {
Person per [] = new Person[3] ; // 對(duì)象數(shù)組
per[0] = new Person("張三",20) ;
per[1] = new Person("李四",20) ;
per[2] = new Person("王五",20) ;
for (int x = 0 ; x < per.length ; x ++) {
System.out.println(per[x].getInfo()) ;
}
}
}
//靜態(tài)初始化
class Person {
private String name ;
private int age ;
public Person(String name,int age) {
this.name = name ;
this.age = age ;
}
public String getInfo() {
return "姓名:" + this.name + "、年齡:" + this.age ;
}
// setter、getter略
}
public class ArrayDemo {
public static void main(String args[]) {
Person per [] = new Person[] {
new Person("張三",20) ,
new Person("李四",20) ,
new Person("王五",20)} ; // 對(duì)象數(shù)組
for (int x = 0 ; x < per.length ; x ++) {
System.out.println(per[x].getInfo()) ;
}
}
}
對(duì)于對(duì)象數(shù)組而言,上述代碼只是更換了一種所謂的數(shù)組定義的類型,但是內(nèi)存圖變得復(fù)雜:

總結(jié)
數(shù)組最大缺陷:
長(zhǎng)度是固定的;
優(yōu)勢(shì):線性保存,根據(jù)索引訪問(wèn),速度較塊。(時(shí)間復(fù)雜度為“1”)。
面試題:定義類的時(shí)候什么情況下會(huì)考慮使用static方法?
在類中不提供任何成員屬性的情況下,如果定義的是普通方法,那么就必須通過(guò)實(shí)例化對(duì)象來(lái)進(jìn)行調(diào)用,這樣就會(huì)產(chǎn)生許多無(wú)用的實(shí)例化對(duì)象。那么在這樣的情況下會(huì)考慮直接定義static方法,這樣可以由類名稱直接調(diào)用。
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)組操作技巧總結(jié)》、《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring?Boot?接口加解密功能實(shí)現(xiàn)
在我們?nèi)粘5腏ava開(kāi)發(fā)中,免不了和其他系統(tǒng)的業(yè)務(wù)交互,或者微服務(wù)之間的接口調(diào)用;如果我們想保證數(shù)據(jù)傳輸?shù)陌踩?,?duì)接口出參加密,入?yún)⒔饷?,這篇文章主要介紹了Spring?Boot?接口加解密功能實(shí)現(xiàn),需要的朋友可以參考下2023-04-04
Java操作MongoDB數(shù)據(jù)庫(kù)的示例代碼
這篇文章主要介紹了Java操作MongoDB的示例代碼,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-04-04
springboot2如何集成ElasticSearch6.4.3
這篇文章主要介紹了springboot2如何集成ElasticSearch6.4.3問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot?Profiles?多環(huán)境配置及切換
大部分情況下,我們開(kāi)發(fā)的產(chǎn)品應(yīng)用都會(huì)根據(jù)不同的目的,所以需要支持不同的環(huán)境,本文主要介紹了SpringBoot?Profiles?多環(huán)境配置及切換,感興趣的可以了解一下2021-12-12
Maven Plugins報(bào)錯(cuò)的解決方法
本文主要介紹了Maven Plugins報(bào)錯(cuò)的解決方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
SpringBoot詳細(xì)探究講解默認(rèn)組件掃描
在項(xiàng)目中我們創(chuàng)建了Controller,這個(gè)Controller是如何被spring自動(dòng)加載的呢?為什么Controller必須放在啟動(dòng)類的同級(jí)目錄下呢2022-06-06
在Java中關(guān)閉SQL執(zhí)行日志來(lái)優(yōu)化服務(wù)器性能
Java應(yīng)用程序中,數(shù)據(jù)庫(kù)操作是一個(gè)常見(jiàn)的任務(wù),如果不適當(dāng)?shù)靥幚鞸QL執(zhí)行日志,可能會(huì)導(dǎo)致不必要的性能損失,SQL執(zhí)行日志通常由數(shù)據(jù)庫(kù)連接池、ORM框架(如Hibernate、MyBatis)、或者應(yīng)用服務(wù)器的內(nèi)置日志機(jī)制生成,本文將探討如何在Java中關(guān)閉SQL執(zhí)行日志,提升應(yīng)用性能和效率2024-11-11

