數組
基本概念
是相同資料類型元素的集合
一組資料,是一種線性結構的資料結構
線性結構:在記憶體中是連續的
數組本身是引用資料類型,即對象。
數組可以存儲基本資料類型,也可以存儲引用資料類型。
連結清單:不是連續的(是線性)
特點
連續
每個元素都有下标,可以快速查找
從中間添加,删除比較麻煩
聲明
資料類型 [] 數組名字; (建議使用)
資料類型 數組名字 [];
建立
聲明同時指定長度,但其元素值都為預設初始值
int[] a= new int[10];
聲明數組并配置設定記憶體将其初始化
int[] a= new int[]{1,2,3,4};
與上面相似但相對簡略
int[] a= {1,2,3,4};
Java中數組在建立時必須指定長度,長度一旦給定就不能改變
動态建立
沒有為元素指派,可結合for循環指派
int[] a= new int[10];
String[] b= new String[12];
靜态建立
在建立的時候已經為元素複制了
int[] a= new int[]{1,2,3,4};
int[] a= {1,2,3,4};
輸出
數組的輸出:
System.out.println(Arrays.toString(數組名))
數組中元素的通路通過下标(索引);
索引資料類型為整型
索引最大值與數組長度差一
數組中的下标是從0開始的
package day2;
import java.util.Arrays;
public class Shuzu {
public static void main(String[] args) {
int[] a= new int[4];
int[] b= new int[]{1,2,3,4};
int[] c= {1,2,3,4,5};
//把數組以字元串的形式輸出
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
System.out.println(Arrays.toString(c));
System.out.println(a[1]);
System.out.println(b[1]);
System.out.println(c[1]);
System.out.println(a.length);
}
}
數組的長度
數組名.length
數組的通路與疊代
疊代
for循環
增強for循環
for(數組元素類型 臨時變量名 : 數組名){
System.out.print(臨時變量名)
}
package day2;
public class ShuzuDiedai {
public static void main(String[] args) {
int[] a={1,2,3,4,5,6,7,8};
//for循環
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
System.out.println();
//增強for循環
for (int i:a) {
System.out.print(i);
}
}
}
數組排序
冒泡排序
相鄰兩個元素進行比較,小的往前冒,大的向後退
package day2;
import java.util.Arrays;
//冒泡排序
public class MaoPaoPaiXu {
public static void main(String[] args) {
int[] a={5,4,8,3,1,2,9,6};
//4,5,3,1,2,8,6 9
//4,3,1,2,5,6 8,9
//3,1,2,4,5 6,8,9
//1,2,3,4 5,6,8,9
//1,2,3 4,5,6,8,9
//1,2 3,4,5,6,8,9
//1 2,3,4,5,6,8,9
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length-1-i; j++) {//每次把最大的退到最後,是以比上次少一次
if (a[j]>a[j+1]){
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
選擇排序
從元素中找出最大或最小的元素放在首位,然後從剩下的元素中找出最大或最小的元素放在已經排序的後面以此類推直到最後一個元素
package day2;
import java.util.Arrays;
//選擇排序
public class XuanZePaiXu {
public static void main(String[] args) {
int[] a= {3,2,5,1,6,8};
//1 2,5,3,6,8
//1,2 5,3,6,8
//1,2,3 5,6,8
//1,2,3,5 6,8
//1,2,3,5,6 8
for (int i = 0; i < a.length-1; i++) {
int temp= i;
for (int j = i+1; j < a.length-1; j++) {
if (a[j]<a[temp]){
temp=j;
}
}
int b;
b=a[i];
a[i]=a[temp];
a[temp]=b;
}
System.out.println(Arrays.toString(a));
}
}
插入排序
假設前n-1個元素已經排好序,現将第n個元素插入進來并且也是排好序的
package day2;
import java.util.Arrays;
//插入排序
public class ChaRuPaiXu {
public static void main(String[] args) {
int[] a={1,5,3,8,4,6,2};
//1,3,5 8,4,6,2
//1,3,5,8 4,6,2
//1,3,4,5,8 6,2
//1,3,4,5,6,8 2
//1,2,3,4,5,6,8
for (int i = 1; i < a.length-1; i++) {
for (int j = i+1; j >=0; j--) {
if (a[j-1]>a[j]){
int temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
二維數組
定義
二維數組的每一個元素都是一個一維數組
聲明
int[] [] a;(建議使用)
int a[] [];
建立
int [] []a=new int[3] [4];
二維數組中一維數組大小可不定義{{null},{null},{null}}即:int [] []a=new int[3] [];
int [] []b=new int[] []{{1,2,3},{1,2,4},{2,0,0,}};
int [] []b={1,2,3},{1,2,4},{2,0,0,}};
疊代
兩個for循環
package day2;
public class Erwei {
public static void main(String[] args) {
int [][]a={{2,0,0,0},{0,8,0,4},{1,9,9,9},{0,8,2,6}};
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
稀疏數組
稀疏數組的大小與二維數組中有效數字個數有關
稀疏數組中一維數組的大小固定為3;
int [] []a=new int[有效數字個數+1] [3];
稀疏數組第一行分别是———二維數組.length 二維數組中一維數組.length 有效數字個數
其他行則表示———有效數字在二維數組中的索引 有效數字在二維數組的一維數組中的索引 值
package day2;
public class XishuShuzu {
public static void main(String[] args) {
XishuShuzu x=new XishuShuzu();
int [][]a=new int[10][10];
a[1][1]=5;
a[2][2]=2;
a[3][3]=1; a[8][4]=5;
a[0][8]=2;
a[2][6]=1; x.Xianshi(a);
//計算稀疏數組中有效内容的數量
int count=0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j]!=0){
count++;
}
}
}
//定義稀疏數組第一行的數值
int [][]b=new int[count+1][3];
b[0][0]=a.length;
b[0][1]=a[1].length;
b[0][2]=count;
//給稀疏數組其他行指派
int k=1;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j]!=0){
b[k][0]=i;
b[k][1]=j;
b[k][2]=a[i][j];
k++;
}
}
}
x.Xianshi(b);
//稀疏數組轉化為二維數組
int [][]c=new int[ b[0][0] ][ b[0][1] ];
for (int i = 1; i < b.length; i++) {
c[ b[i][0] ][ b[i][1] ]=b[i][2];
}
x.Xianshi(c);
}
//顯示數組
public void Xianshi(int [][] a){
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}