数组是在编程中最基础且重要的数据结构,但是简单的东西一定要掌握牢固并且熟练运用,这对之后的工作学习会有很多帮助。Kotlin语言和Java很相似,都是基于JVM的,而Kotlin语法灵活度更高 、代码量更小。在Android开发中Kotlin成为谷歌官方推荐语言,一定程度上取代了Java在移动端开发的位置。之后我会整理一下常用的数据结构在Java和Kotlin的应用,做一下对比。既是我自己的学习笔记,又可以让初步学习的朋友们有一个参考。
Java中的数组
1、初始化定义方法:
数据类型[] = new 数据类型[数组长度];
数据类型[] = new 数据类型[]{字段1,字段2,……};
数据类型[] = {字段1,字段2,……};
第一种方法只是定义了数组的长度,而对数组的字段内容没有进行编辑;第二、三种将数组内容和长度都确定了。下面举具体的例子来进行说明:
int[] a = new int[]{1,2,3};
String[] b = {"aa","bb","cc"};
boolean[] c = new boolean[5];
例子很简单就不详细说了。
2、数组的遍历
数组定义好了,那么怎样取得数组中的数据呢?
有两种方法,一种是通过数组索引将对应的值取出来,取值方法格式为:
数据类型 定义的新变量 = 数组[索引位置]
举一个简单的例子:
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,2,3};
int d = a[1];
System.out.println(d);
}
}
运行结果为:2。这段代码也可以写为:
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,2,3};
System.out.println(a[1]);
}
}
取数组数据的第二种方法就是数组遍历,数组遍历有三种方法
a, for循环遍历
这是最简单的一种遍历方法,语法格式为:
for(数据类型:参数初始定义 ;遍历条件; 参数变化方式){
}
举一个简单的例子:
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,2,3};
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+" ");
}
}
}
运行结果为:1 2 3 。a.length表示a数组的长度,例子里面为3,i为数组的索引,取值范围是[0,1,2]。
b, for-each遍历
for-each遍历相比于for循环更加简洁,并且不需要利用索引来或许元素,语法格式为:
for(数据类型 参数: 数组){
}
举一个简单的例子:
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,2,3};
for(int i: a) {
System.out.print(i+" ");
}
}
}
运行结果也是:1 2 3 。
c, Array类里面的toString静态方法
举一个简单的例子:
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,2,3};
System.out.print(Arrays.toString(a));
}
}
调用了Arrays.toString(数组)的方法,导入java.util.Arrays类。运行结果为:[1, 2, 3]。
3、数组的常用方法
1、array.length
获得数组的长度,之前有例子,很简单就不具体介绍。
2、Arrays.toString
遍历数组,之前也介绍过
3、Arrays.sort
这个方法中有两个构造方法,分别是:
Arrays.sort(int[] a)
Arrays.sort(int[] a, int fromIndex, int toIndex)
第一个是对整个数组进行排序,第二个是对索引区间进行排序。举一个例子:
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,42,23,6,24,5};
Arrays.sort(a,2,4);
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
运行结果是
[1, 42, 6, 23, 24, 5]
[1, 5, 6, 23, 24, 42]
4、Arrays.copyOf与Arrays.copyOfRange
这两个都是对数组进行复制的方法,在代码中进行介绍:
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,42,23,6,24,5};
int[] b = Arrays.copyOf(a, 10);//对a数组进行复制,并且定义数组b的长度为10
System.out.println(Arrays.toString(b));
int[] c = Arrays.copyOfRange(a, 1, 4);//将a数组从索引1到索引4(不包括索引4)的数据复制到数组c上
System.out.println(Arrays.toString(c));
}
}
运行结果为:
[1, 42, 23, 6, 24, 5, 0, 0, 0, 0]
[42, 23, 6]
5、Arrays.fill
这个方法有两个构造函数,在代码中进行具体描述
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,42,23,6,24,5};
Arrays.fill(a, 111);//将111填充到a的所有元素
System.out.println(Arrays.toString(a));
Arrays.fill(a, 0,3,222);//将222填充到a的0索引到3索引(不包括3索引)处
System.out.println(Arrays.toString(a));
}
}
运行结果为:
[111, 111, 111, 111, 111, 111]
[222, 222, 222, 111, 111, 111]
6、Arrays.asList
这个方法是将一个数组转换为List,举一个简单例子:
public class Array {
public static void main(String args[]) {
String[] a = {"aa","bb","cc"};
List<String> b = Arrays.asList(a);
System.out.println(b);
Integer[] c = new Integer[]{5,4,3,2,1};
List<Integer> d = Arrays.asList(c);
}
}
例子里面讲两个数组转换为了List,需要注意的是List中不能存放int对象,存放的是Integer对象,Integer是int的封装类。
7、Arrays.binarySearch
这个方法的作用是用二分法查找数组中的元素,返回它的索引;如果没有这个元素,返回-1。但是在用Arrays.binarySearch方法前一定要将数组进行排序,要不然会不准。
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,42,23,6,24,5};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
int b = Arrays.binarySearch(a,6);
System.out.println(b);
}
}
输出的结果为:
[1, 5, 6, 23, 24, 42]
2
8、Arrays.equals(a,b)
这个方法是判断两个数组是否相同
public class Array {
public static void main(String args[]) {
int[] a = new int[]{1,42,23,6,24,5};
int[] b = new int[]{1,42,23,6,24,5};
int[] c = new int[]{2,42,23,6,24,5};
boolean d = Arrays.equals(a, b);//判断数组a和b是否相等
boolean e = Arrays.equals(a, c);//判断数组a和c是否相等
System.out.println(d);
System.out.println(e);
}
}
输出的结果为:truefalse
kotlin中的数组
1、初始化
val a = arrayOf("1","2","3")//定义一个数组,并填充元素
val b = IntArray(2)//定义一个长度为2的整型数组,不填充元素
val c = intArrayOf(22,33,11)//定义一个整型的数组,并填充元素
这是三种定义数组的方式,可以看到在等号前面没有定义元素的类型,这是因为kotlin中可以自动识别匹配数据类型,等号右面的类型给到了左边的元素。
val b = IntArray(2)//定义一个长度为2的整型数组,不填充元素
b[0] = 1
b[1] = 2
for(i in b){
print(" "+i)
}
上面的代码块中通过索引,将元素值传入数组中,输出结果为:1 2
2、遍历
val c = intArrayOf(22,33,11)//定义一个整型的数组,并填充元素
//for-in用元素来遍历
for(i in c){
print(" "+i)
}
println("///")
//for-in用索引来遍历
for(index in c.indices){
print(" "+c[index])
}
println("///")
//索引和元素同时遍历
for((index,value) in c.withIndex()){
print("index: $index, value: $value, ")
}
上面的代码是遍历数组的三种方式。输出的结果为:
22 33 11///
22 33 11///
index: 0, value: 22, index: 1, value: 33, index: 2, value: 11,
3、常用方法
相对于Java的数组,kotlin中有两处需要注意的。数组的长度:array.size,数组的索引:array.indices
val c = intArrayOf(22,33,11)//定义一个整型的数组,并填充元素
val d = c.size//数组长度
print(d)
for(index in c.indices){//数组索引
print(" "+c[index])
}
上述Java中介绍的其他方法,只要导入java.util.Arrays用法都是相同的。