JAVA程序設計——數組、字符串(String)

前言:

數組與字符串 (說好一週一更的呢?

從這篇開始,不在使用截圖驗證方式,代碼驗證似乎更讓人懂。

目錄

一、數組

1.數組的創建與賦值

Ⅰ:數組聲明(兩種)

Ⅱ:數組分配內存

Ⅲ:數組初始化

2.數組的遍歷

Ⅰ:基礎遍歷

Ⅱ:for-eash 增強for循環

3.數組的基本操作‘

Ⅰ.數組的copy

Ⅱ.與Arrays有關的操作

4.可變長參數數組與多維數組

Ⅰ:可變長參數數組

Ⅱ:多維數組

字符串String

1.字符串的創建:


一、數組


1.數組的創建與賦值

數組創建具備以下過程:

Ⅰ:數組聲明(兩種)

int[] scores;
int scores[];
////創建一個int類型的socres數組

創建時提倡第一種聲明方法,同時注意:數組聲明時並沒有像C++那樣被分配內存,所以不可將代碼寫爲:

int[5] scores;

Ⅱ:數組分配內存

int[] scores;
scores = new int[8];
////創建了一個大小爲8的int類型數組

並且上述兩個語句可以連起來使用:int[] scores = new int[8]

Ⅲ:數組初始化

分配內存之後,接下來的操作便是對數組進行初始化。

初始化有兩種方法:

int[] scores = {1,2,3};
///靜態初始化,{}內跟scores具有的值
int[] scores=new int[]{1,2,3};
///此時注意,[]內不要有數組長度大小的值
///系統會自動分配

完成以上三步,一個數組就創建OK了。


2.數組的遍歷

java中數組的遍歷與C++比較像(可以說具有某種共性)

Ⅰ:基礎遍歷

我們可以像C++一樣,根據下標去訪問數組中的第index個元素:

for(int i = 0 ;i < scores.length;i++){
	System.out.println(i);
}

注意:爲了防止數組越界情況發生,建議使用length函數去訪問,這樣即安全又有利於維護。

Ⅱ:for-eash 增強for循環

在C++中遍歷迭代器等容器:例如vector,set等 ,用到過此類增強for循環:

for(int x:scores) 
	System.out.println(x);
////格式 for(類型  變量:數組)
////注意此時的類型應與數組類型相同

並做個簡單的測試,通過增強for循環不會改變數組內部的值:

public class Test{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] scores= {1,2,3};
		for(int x:scores) {
			System.out.print(x+" ");
			x++;
		}
		System.out.println();
		for(int x:scores) 
			System.out.print(x+" ");
	}
}
//輸出結果
//1 2 3
//1 2 3
///由此可見 通過改變x並不會改變java的數組

3.數組的基本操作‘

Ⅰ.數組的copy

首先思考一個問題:

public class Test{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] scores= {1,2,3};
		int[] copyscores ;
		copyscores = scores;
		for(int x:copyscores)
			System.out.print(x+" ");
		System.out.println();
	}
}
///以上輸出結果爲 1 2 3

但是這是不是就意味着,已經完成了數組的copy呢?

我們將代碼增加以下修改操作:

public class Test{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] scores= {1,2,3};
		int[] copyscores ;
		copyscores = scores;
		for(int x:copyscores)
			System.out.print(x+" ");
		System.out.println();
		copyscores[0]=2;
		for(int x:scores)
			System.out.print(x+" ");
		System.out.println();
		for(int x:copyscores)
			System.out.print(x+" ");
		System.out.println();
	}
}
///以上輸出結果爲:
///1 2 3
///2 2 3
///2 2 3

這就意味着,在改變copy數組的同時,原數組也發生了改變,說明 原數組與copy數組指向了同一個內存地址,所以數組賦值語句只是將scores數組的一個引用賦值給了copyscores,並沒有實現真正的copy。

如何實現真正的copy?

第一種方法:for循環(非常簡單也可行的方法)

int[] scores= {1,2,3};
int[] copyscores = new int[4];
for(int i=0;i<scores.length;i++)
		copyscores[i]=scores[i];
////這樣就非常簡單的實現了數組的copy
////同樣 如果 將上述測試移到這裏,發現修改scores時,copyscores並沒有改變。

第二種方法:System.arraycopy類的方法

System.arraycopy(src, srcPos, dest, destPos, length);
///src 被複制數組名
///srcpos 從哪開始複製
///dest 複製數組名
///destpos 從哪開始複製
///length 複製長度
example:
int[] scores= {1,2,3};
int[] copyscores = new int[4];
System.arraycopy(scores, 0, copyscores, 0, 1);
///scores : 1 2 3
///copyscores :1 0 0
System.arraycopy(scores, 1, copyscores, 0, 2);
///scores : 1 2 3
///copyscores :2 3
System.arraycopy(scores, 1, copyscores, 1, 2);
///scores : 1 2 3
///copyscores :0 2 3

詳情見以上代碼

Ⅱ.與Arrays有關的操作

數組排序:Arrays.sort(數組名)

int[] scores= {3,2,1};
Arrays.sort(scores);
///scores:1 2 3

需要值得一說的是:Arrays.sort默認爲 升序排序,並且對所有可比較的數據類型,都可以進行升序排序。

但是此方法也支持,各種類型的排序具體的排序類型需要自行構建比較對象comparator。

數組填充:Arrays.fill(name,begin,end ,val)

int[] copyscores = new int[4];
Arrays.fill(copyscores,0,3,2);
///輸出結果:
///2 2 2 0

數組相等:Arrays.equal(name1,name2)

int[] scores= {3,2,1};
int[] copyscores = new int[4];
System.arraycopy(scores, 0, copyscores, 0, 3);
boolean f= Arrays.equals(scores, copyscores);
System.out.print(f);
////輸出結果爲false
///因爲copy數組大小聲明爲4

4.可變長參數數組與多維數組

Ⅰ:可變長參數數組

這個應用在實際中非常的重要,舉個例子:

請你用一個方法使用傳參的形式計算出4個數相加的結果,那麼就需要傳4個參數

   public long GetSum(int a,int b,int c,int d) {
	   long sum=a+b+c+d;
	   return sum;
	}

如果參數個數類型不一:5個6個3個等,這時候用方法重載的辦法也會降低效率。

最好的辦法就是 —— 可變長參數數組

   public static long GetSum(int...scores) {
	   long sum=0;
	   for(int x:scores)
		   sum+=x;
	   return sum;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(GetSum(1,2,3));
		System.out.println(GetSum(1,2,3,4));
	}
///輸出結果:
///6
///10
///可見十分方便~

Ⅱ:多維數組

多維數組可以理解爲:一維的一維的一維的一維的一維(....可變長參數)

所以聲明與上述聲明方法類似,唯一不同在於,每一維空間都可不相同。

聲明方法:int[][][] s= new int[][][]///有幾維 就幾個[]

不定長在於:

int[][] s= new int[4][];
s[0] = new int[2];
s[1] = new int[3];
///注意不定長的一維一定要不帶參數

字符串String

1.字符串的創建:

感覺常用的就兩種創建:

String a ="123";
String b = new String();
b="321";
System.out.println(a);
System.out.println(b);
///123
///321

特別注意String是一種靜態的不可改變的數組,如果想要改變String的可以使用Stringbuffer。

StringBuffer a = new StringBuffer();
a.append(2);
System.out.println(a);

2.字符串String類的附帶操作:

發現一個大佬整理的特別強勢,也因爲此時已經沒時間了

String的操作集合

 


感謝閱讀啦,本系列爲期末考試而生,有幸幫到你是榮幸。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章