(四)數組和字符串

目錄

一、數組的創建和初始化

二、for-each循環

三、數組操作與Arrays類

四、二維數組

五、字符串String


一、數組的創建和初始化

1、創建語法:

2、初始化:

①靜態初始化

float[ ] scores = {1,2,3,4};
String[ ] str ={"How", "are","you"};

②動態初始化

float[ ] scores = new float[ ]{21.4f, 56.9f};

注意:定義int型數組後每個元素默認初始化0,boolean型默認爲false。

二、for-each循環

1、含義:在訪問數組中元素時,可使用for循環通過下標確定元素值,也可使用for-each循環優點是可避免下標訪問異常。

2、語法:

package day11;

public class Test {
	public static void main(String[] args) {
		int[] num=new int[]{1,2,3};
		System.out.println("使用for循環輸出數組中元素:");
		for(int i=0;i<num.length;i++) {
			System.out.print(num[i]+" ");
		}
		System.out.println("\n使用for-each循環輸出數組中元素:");
		for(int nums:num) {
			System.out.print(nums+" ");
		}
	}
}

運行結果: 

 

注意:

①上例中for-each循環括號中nums表示數組單個元素的類型。

②修改nums的值不會影響num數組中元素的值。

package day11;

import java.util.Arrays;

public class Test {
	public static void main(String[] args) {
		int[] num=new int[]{1,3,2};
		System.out.println("修改前數組中的元素值:");
		for(int i=0;i<num.length;i++) {
			System.out.print(num[i]+" ");
		}
		System.out.println("\n修改nums的值!");
		for(int nums:num) {
			nums++;
		}
		System.out.println("修改後數組中的元素值:");
		for(int i=0;i<num.length;i++) {
			System.out.print(num[i]+" ");
		}
	}
}

運行結果:

三、數組操作與Arrays類

1、數組排序

package day11;

import java.util.Arrays; //sort方法在Arrays類中

public class Test {
	public static void main(String[] args) {
		int[] num=new int[]{1,3,2};
		Arrays.sort(num);
		System.out.println("排序後的元素順序爲:");
		for(int nums:num) { //for-each循環輸出元素
			System.out.print(nums+" ");
		}
	}
}

補充:Math.random()生成隨機數

Math.random(); //隨機生成[0~1)之間的浮點型
(int)(Math.random()*100); //隨機生成[0~100)之間的整數

2、傳數組類型的參數:

package day11;

public class Test {
	public static void main(String[] args) {
		int[] num=new int[]{1,3,2};
		showInfo(num); //showInfo使用static修飾纔可以直接使用方法名調用方法
	}
	public static void showInfo(int[] x) {
		System.out.println("參數數組中的元素:");
		for(int y:x) {
			System.out.print(y+" ");
		}
	}
}

四、二維數組

1、理解:

注意:N*N的二維數組就可以想象成一個N*N矩陣,其中橫縱座標都是從0開始(這個必須得習慣)。

2、創建:

package day11;

public class Test {
	public static void main(String[] args) {
		int[][] num=new int[][]{{1,2,3},{4,5,6},{7,8,9}};
	}
}

理解圖:

3、二維數組的遍歷:

package day11;

public class Test {
	public static void main(String[] args) {
		int[][] num=new int[][]{{1,2,3},{4,5,6},{7,8,9}};
		for(int[] nums:num) {
			for(int y:nums) {
				System.out.print(y+" ");
			}
			System.out.println();
		}
	}
}

運行結果:

五、字符串String

1、創建:

package day11;

public class Test {
	public static void main(String[] args) {
		String str=new String("hello world");
	}
}

2、常用方法

①獲取長度:

str.length()

②比較大小(字典序):

str.compareTo("nihao"); //大於返回正值,小於返回負值,等於返回0
str.compareToIgnoreCase("NIHAO");//不區分大小寫比較

③判斷str1是不是str的子串:

str.concat(str1); //返回值爲boolean類型

④返回首次出現str1的位置下標:

str.indexOf(str1); //從頭開始檢索
str.indexOf(str1,index); //從index位置開始檢索
str.lastIndexOf(str1);//從後向前開始檢索

⑤拼接:

str.concat("!");//末尾處拼接

⑥截取:

str.substring(start,end);//截取區間[start,end)

3、'=='操作符

①基本類型值比較時,只要兩個變量值相等即爲true

②其餘類型比較時,只有兩個變量來自同一個對象時才爲true

③使用==操作符判斷時,兩邊的數據類型要兼容,否則編譯錯誤

案例:判斷返回值

int it = 65;
float fl = 65.0f;
System.out.println(“65和65.0f是否相等?” + (it == fl)); //true

4、equals方法

對類File、String、Date及包裝類(Wrapper Class)來說,是比較類型及內容而不考慮引用的是否是同一個對象。

注意:equals方法僅能用於引用類型,因爲equals方法是object中的方法。

補充(String對象的創建方式):

方式1:

String str1="hello world";
String str2="hello world";

注意:該方式創建的兩個字符串變量內存位置相同,所以若使用==判斷,結果爲true。

方式2:

String str1=new String("hello world");
String str2=new String("hello world");

注意:使用該方法創建的字符串內存不同,使用==判斷返回false。

5、String和其它數據類型轉換

①字符串轉換爲其它數據類型:

②其它數據類型轉換爲字符串:

不足請留言評論,謝謝!

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