(四)数组和字符串

目录

一、数组的创建和初始化

二、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("
使用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("
修改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和其它数据类型转换

①字符串转换为其它数据类型:

②其它数据类型转换为字符串:

不足请留言评论,谢谢!

原文地址:https://www.cnblogs.com/ldu-xingjiahui/p/12594032.html