Java開發(fā)必備知識之數組詳解
更新時間:2021年06月01日 10:40:31 作者:北夷煙雪江中雨
數組對于每一門編程語言來說都是重要的數據結構之一,當然不同語言對數組的實現及處理也不盡相同.本篇文章為大家整理了Java最全關于數組的知識點,并給出其對應的代碼,需要的朋友可以參考下
一、ASCII碼

二、為什么需要數組
案例: 160班 現在 77人 統計 全班的Java成績 用程序進行存儲 變量 統計 全班不及格的同學 要 補考 補考過的同學 修改成績 定義 77 個變量 int 帥 = 59; int 洋 = 100; int cto = 60; int ceo = 58;
三、什么是數組
概念:數組就是內存中一塊 連續(xù)的 內存空間,用于存放 相同類型 的多個數據
四、定義數組
聲明一個數組:確定數組中存放的數據類型
語法:
數據類型[] 數組名;//建議
數據類型 []數組名;
數據類型 數組名[];//c語言的寫法 不建議
案例:
//聲明一個數組
int[] a ;
為數組開辟空間:確定了數組的容量大小
語法:數組名 = new 數據類型[數組的長度];
案例://為數組開辟空間
a = new int[77];
數組的長度:自動獲取數組的長度 數組名.length
數組的下標:為每個數組的元素分配一個標號 0------數組名.length-1
數組的元素:數組中每個小空間叫做數組的一個元素相當于一個變量
定義數組的其他方式
第一種:聲明 開辟空間(隱式)
1)先聲明 后開辟空間
語法:
數據類型[] 數組名;//聲明
數據名 = new 數據類型[數組長度];//開辟空間
2)聲明的同時并開辟空間
語法:數據類型[] 數組名 = new 數據類型[數組長度];
第二種:聲明 賦值(顯式初始化)
1)先聲明 后賦值
語法:
數據類型[] 數組名 ;//聲明
數組名 = new 數據類型[]{元素1,元素2,元素3,。。。};//賦值
2)聲明的同時并賦值
語法:數據類型[] 數組名 = new 數據類型[]{元素1,元素2,元素3,。。。};
簡寫形式:數據類型[] 數組名 = {元素1,元素2,元素3,。。。};
注意:
1.簡寫形式不能分為兩行 數組的聲明和賦值必須在同一行
2.在定義數組的過程中 不能既賦值同時又定義長度
五、使用數組
存數據:為每個元素賦值
取數據:
語法:數組名[數組下標] = 值;
案例:
public class Demo{
public static void main(String[] args) {
//聲明一個數組
int[] a ;
//為數組開辟空間
a = new int[4];
//存數據
a[0] = 2;
a[1] = 4;
a[2] = 6;
a[3] = 8;
//訪問數據
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
//獲取數組的長度
System.out.println("數組的長度:"+a.length);
}
}
六、數組的默認值
當定義一個數組沒有為數組賦值數組的每一個元素都有一個默認值 默認值的類型和數組的類型是相關的
簡單數據類型
byte--------------0
short-------------0
int -------------0
long--------------0
float ------------0.0
double------------0.0
char-------------空字符 '\u0000'
boolean----------false
引用數據類型(對象類型)------null
案例:
public class Demo{
public static void main(String[] args) {
//定義一個數組
String[] a = new String[5];
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
System.out.println(a[4]);
}
}
七、數組的遍歷
遍歷:訪問數組中的每個元素并打印
案例:
public class Demo{
public static void main(String[] args) {
int[] a = {3,5,6,7,8,3,23,56,78,52,34};
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+"\t");
}
}
}
八、數組在內存中的分析
int[] a = new int[4];
a的引用指向 數組在堆內存中的實體 (見圖示)
元素的地址值 = 首地址+數據類型字節(jié)數*下標
案例:
public class Demo{
public static void main(String[] args) {
int[] a = new int[3];
a[0] = 2;
a[1] = 3;
a[2] = 5;
int[] b = new int[4];
b = a;
System.out.println("b數組的長度:"+b.length);
System.out.println("a數組的長度:"+a.length);
for(int i =0;i<b.length;i++){
System.out.print(b[i]+" ");
}
}
}
九、數組的擴容
思想:
1.定義一個新的數組 容量要比之前的數組大
2.將原數組中的元素 復制到新數組中
3.將原數組的引用指向新數組
注意:如果超出了數組的下標 那么會報異常
java.lang.ArrayIndexOutOfBoundsException 數組下標越界異常
方法:
=========================================================================================
第一種:自己寫
代碼:
public class Demo{
public static void main(String[] args) {
int[] a = {2,4,6,3,7,5,23};//7
System.out.println("擴容之前的長度:"+a.length);
//1,定義一個新數組
int[] b = new int[a.length*2];
//2,將原數組元素復制到新數組中
for(int i = 0;i<a.length;i++){
b[i] = a[i];
}
//3 將a的引用指向新數組
a = b;
//遍歷a數組
System.out.println("擴容之后的長度:"+a.length);
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
=================================================================
第二種:利用 System.arraycopy(原數組名,原數組的起始下標,新數組名,新數組的起始下標,要復制的長度)
代碼:
public class Demo{
public static void main(String[] args) {
int[] a = {2,4,6,3,7,5,23};//7
System.out.println("擴容之前的長度:"+a.length);
//1,定義一個新數組
int[] b = new int[a.length*2];
//2,將原數組元素復制到新數組中
System.arraycopy(a,0,b,0,a.length);
/*for(int i = 0;i<a.length;i++){
b[i] = a[i];
}
*/
//3 將a的引用指向新數組
a = b;
//遍歷a數組
System.out.println("擴容之后的長度:"+a.length);
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
==================================================================
第三種:利用Arrays工具類 操作
Arrays是java.util包中的一個工具類
Arrays.copyOf(原數組,擴容之后的長度) 此函數可以返回一個新的擴容之后的數組數組長度由函數的第二個參數決定
代碼:
import java.util.*;
public class Demo{
public static void main(String[] args) {
int[] a = {2,4,6,3,7,5,23};//7
System.out.println("擴容之前的長度:"+a.length);
a = Arrays.copyOf(a,a.length*2);
System.out.println("擴容之后的長度:"+a.length);
//遍歷
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
練習:隨機點名系統
import java.util.*;
public class Demo{
public static void main(String[] args) {
String[] s = {"",""};
//系統隨機產生一個下標
int index = (int)(Math.random()*s.length);
System.out.println(s[index]);
}
}
案例:隨機賦值
import java.util.*;
public class Demo{
public static void main(String[] args) {
int[] a = new int[10];
//循環(huán)賦值
for(int i = 0;i<a.length;i++){
a[i] = (int)(Math.random()*100);
}
//遍歷
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
十、數組用在函數的參數上
案例:寫一個函數 函數的功能是遍歷數組 形參類型 數組類型 參數幾個 1 返回值 void printArray
代碼:
import java.util.*;
public class Demo{
public static void main(String[] args) {
int[] a = {3,4,56,7,8,8,9,9,3,5,66,77,88};
printArray(a);
System.out.println();
int[] b = new int[10];
printArray(b);
}
//
public static void printArray(int[] a) {
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+",");
}
}
}
數組用在函數的參數和返回值上
案例:寫一個函數 實現數組的擴容
代碼:
import java.util.*;
public class Demo{
public static void main(String[] args) {
int[] a = {3,4,56,7,8,8,9,9,3,5,66,77,88};
//調用 擴容
a = myCopyOf(a);
printArray(a);
}
//
public static void printArray(int[] a) {
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+",");
}
}
//擴容數組
public static int[] myCopyOf(int[] a) {
//定義一個新數組
int[] b = new int[a.length*2];
//將原數組的元素復制到新數組
for(int i = 0;i<a.length;i++){
b[i] = a[i];
}
return b;
}
}
案例:定義一個函數 函數的功能是 計算多個數的和 數不定
代碼:
import java.util.*;
public class Demo{
public static void main(String[] args) {
int[] a = {3,4,5};
int sum = add(a);
System.out.println(sum);
}
//定義一個函數 實現 計算幾個數的和
public static int add(int[] a) {
int sum = 0;
for(int i = 0;i<a.length;i++){
sum+=a[i];
}
return sum;
}
}
十一、函數可變長參數
語法: 數據類型... 變量名
就相當于 數據類型[] 變量名;
public static 返回值類型 函數名(數據類型... 變量名){
}
注意:可變長參數函數中的形參列表只能有一個可變長參數,如果有其他的形參,那么可變長參數必須放在參數列表的最后
案例:
import java.util.*;
public class Demo{
public static void main(String[] args) {
/*int sum1 = add(3,4,5);
int sum2 = add(4,6,9);
int sum3 = add(3,6,8,9,4,3);*/
int sum = add(3,4,6);
System.out.println(sum);
}
//定義一個可變長參數函數
public static int add(int b,int c,int...a) {
int sum = 0;
for(int i = 0;i<a.length;i++){
sum+=a[i];
}
return sum;
}
}
案例:傳數組也可以
import java.util.*;
public class Demo{
public static void main(String[] args) {
int[] a = {1,2,3};
int sum = add(a);
System.out.println(sum);
}
//定義一個可變長參數函數
public static int add(int...a) {
int sum = 0;
for(int i = 0;i<a.length;i++){
sum+=a[i];
}
return sum;
}
}
案例:這樣是不行的
import java.util.*;
public class Demo{
public static void main(String[] args) {
int sum = add(2,4,5);
System.out.println(sum);
}
//定義一個可變長參數函數
public static int add(int[] a) {
int sum = 0;
for(int i = 0;i<a.length;i++){
sum+=a[i];
}
return sum;
}
}
十二、數組的排序
1.4. 6.8.3 ---- 1 3 4 6 8 套路:算法
十三、冒泡排序
規(guī)則:相鄰的兩個元素進行比較 如果前者大于后者 則兩者交換位置
案例: 3,1,4,2 ------->1 2 3 4
***
**
*
雙重for循環(huán)
代碼:
import java.util.*;
public class Demo{
public static void main(String[] args) {
int[] a = {3,5,87,34,67,2,95,36,67,78};
//int[] a = {3,1,4,2};
//冒泡排序
for(int i = 1;i<=a.length-1;i++){
for(int j = 0;j<a.length-i;j++){
if (a[j]>a[j+1]) {
//交換位置
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
//遍歷
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}

選擇排序
規(guī)則:在數組中選擇一個數 和其他的數進行比較 如果這個數大于其他的數 那么兩者交換位置
代碼:
import java.util.*;
public class Demo{
public static void main(String[] args) {
int[] a = {3,5,87,34,67,2,95,36,67,78};
//int[] a = {3,1,4,2};
//冒泡排序
/*for(int i = 1;i<=a.length-1;i++){
for(int j = 0;j<a.length-i;j++){
if (a[j]<a[j+1]) {
//交換位置
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}*/
//選擇排序
for(int i = 0;i<=a.length-2;i++){
for(int j = i+1;j<a.length;j++){
if (a[i]>a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
//遍歷
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}

快速排序
jdk提供的屬于java.util中的Arrays工具類的函數
java.util.Arrays.sort(a);
代碼:
public class Demo{
public static void main(String[] args) {
int[] a = {3,5,87,34,67,2,95,36,67,78};
//int[] a = {3,1,4,2};
//快速排序
java.util.Arrays.sort(a);
//遍歷
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
十四、二維數組
概念:是一維數組的一維數組 數組: 8中基本數據類型 引用數據類型 數組是引用數據類型 數據類型[] -----數組類型
二維數組的定義
數組的聲明:
語法:
數據類型[][] 數組名;
為數組分配空間
語法:
數組名 = new 數據類型[高維數組長度][低維數組長度]
案例:
public class Demo{
public static void main(String[] args) {
int[][] a = new int[4][3];
/*int[] b = new int[4];
b[0] = new int[3];
b[1] = new int[3];
b[2] = new int[3];
b[3] = new int[3];*/
a[0][0] = 1;
a[0][1] = 2;
a[1][2] = 5;
for(int i = 0;i<a.length;i++){
for(int j = 0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
二維數組的遍歷
方法:先遍歷高維 再遍歷低維
代碼:
for(int i = 0;i<a.length;i++){
for(int j = 0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
二維數組的定義的其他方式
第一種:聲明 分配空間
1)先聲明 后分配空間
int[][] a;
a = new int[4][5] //4行5列
2)聲明并分配空間
int[][] a = new int[4][5];
第二種:聲明 賦值
1)先聲明 后賦值
int[][] a ;
a = new int[][]{{2,3,4},{4,5,6},{4,6,8},{3,5,7}}
2)先聲明并賦值
int[][] a =new int[][]{{2,3,4},{4,5,6},{4,6,8},{3,5,7}}
簡寫:int[][] a = {{2,3,4},{4,5,6},{4,6,8},{3,5,7}};
注意:簡寫形式不能分為兩行 聲明和賦值必須在同一行
案例:
public class Demo{
public static void main(String[] args) {
//聲明并賦值
int[][] a = new int[][]{{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
for(int i = 0;i<a.length;i++){
for(int j = 0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
不規(guī)則的二維數組
數組中每個元素數組的長度是不同的
int[][] a = {{1,2,3,3},{4,5},{7,8,9,8,56},{10,11,12}};
遍歷:
public class Demo{
public static void main(String[] args) {
//聲明并賦值
int[][] a = {{1,2,3,3},{4,5},{7,8,9,8,56},{10,11,12}};
for(int i = 0;i<a.length;i++){
for(int j = 0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
注意的問題:
1.當定義一個二維數組 可以先為高維數組進行分配空間,再一一為低維數組進行初始化
案例:
public class Demo{
public static void main(String[] args) {
//聲明并賦值
int[][] a;
a = new int[3][];
a[0] = new int[3];
a[1] = new int[4];
a[2] = new int[5];
for(int i = 0;i<a.length;i++){
for(int j = 0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
2.當定義一個二維數組,必須先為高維分配空間,因為低維數組是依賴于高維數組創(chuàng)建的
案例:
public class Demo{
public static void main(String[] args) {
//聲明并賦值
int[][] a;
a = new int[][4];//錯誤的
for(int i = 0;i<a.length;i++){
for(int j = 0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
十五、引用之間的傳遞

到此這篇關于Java開發(fā)必備知識之數組詳解的文章就介紹到這了,更多相關Java數組內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot使用mybatis一對多的關聯查詢問題記錄
這篇文章主要介紹了springboot使用mybatis一對多的關聯查詢問題記錄,剛好最近有個需求需要做到關聯的查詢,時間也算充足,所以用sql來寫,于是踩了很久坑,終于跳出來了,小小記錄一下2022-01-01
Spring BeanFactory和FactoryBean有哪些區(qū)別
這篇文章主要介紹了Spring BeanFactory 與 FactoryBean 的區(qū)別詳情,BeanFactory 和 FactoryBean 的區(qū)別卻是一個很重要的知識點,在本文中將結合源碼進行分析講解,需要的小伙伴可以參考一下2023-02-02

