数组练习
复合类型数组
动态数组-ArrayList
将一组数中的0排除,返回新的数组,如{19,0,7,5,0,2,0,11,22,32,0},最后形成新数组{19,7,5,2,11,22,32}
public class Demo1 { public int[] delZero(int[] source){ //统计源数组中不为0的数有多少个 int count = 0; for(int i = 0;i < source.length;i++){ if(source[i] != 0){ count++; } } //根据不为零的元素总数创建新数组 int[] newArray = new int[count]; //将count归零,作为新数组的索引 count = 0; for(int i = 0;i < source.length;i++){ if(source[i] != 0){ newArray[count++] = source[i]; } } return newArray; } //局部变量在使用时一定要先初始化 public static void main(String[] args) { int[] source = {19,0,7,5,0,2,0,11,22,32,0}; int[] a = new Demo1().delZero(source); for (int i : a) { System.out.print(i+" "); } } }
将一组数组逆序输出,如:{19,0,7,5,0,2,0,11,22,32,0},输出结果{0,32,22,11,0,2,0,5,7,0,19}
public class Demo2 { public int[] reverse(int[] source){ //创建新数组 int[] newArray = new int[source.length]; for(int i = 0;i < source.length;i++){ newArray[source.length - 1 - i] = source[i]; } return newArray; } public static void main(String[] args) { int[] o = {19,0,7,5,0,2,0,11,22,32,0}; o = new Demo2().reverse(o); for (int i : o) { System.out.print(i+" "); } } }
对一组无序数组完成排序操作(选择排序)
public class Demo3 { public int[] sort(int[] source){ //声明临时变量用于存储交换的值 int temp = 0; for(int i = 0;i < source.length;i++){ //内层循环获取被比较数相邻的后一位数 for(int j = i + 1;j < source.length;j++){ //对比两位数大小,并交换位置 if(source[i] > source[j]){ temp = source[i]; source[i] = source[j]; source[j] = temp; } } } return source; } public static void main(String[] args) { int[] a = {34,12,8,22,5,0,1,3,2,2,5,0}; a = new Demo3().sort(a); for (int i : a) { System.out.print(i+" "); } } }
对一组有序数组进行搜索,查找目标数在数组中的位置(二分法/折半查找)
public class Demo4 { public int binarySearch(int[] arr,int target){ //确定搜索范围(起始查询位置,结束位置) int start = 0; int end = arr.length - 1; //初始化目标数位置 int index = -1; while(start <= end){ //获取中间数索引 int mid = (start + end) / 2; //判断目标数与中间数的大小 if(target > arr[mid]){ start = mid + 1; }else if(target < arr[mid]){ end = mid - 1; }else{ index = mid; break; } } return index; } public static void main(String[] args) { int[] arr = {0 ,0 ,1 ,2 ,2 ,3 ,5 ,5 ,8 ,12 ,22 ,34}; int target = 9; int index = new Demo4().binarySearch(arr, target); System.out.println(target+"所在的位置:"+index); } }
500个人围城一个圈,从1开始报数,每数到3的倍数的人离开圈子,循环往复直到最后圈中只剩下一人为止,求剩下的人原来在圈中的位置(约瑟夫环)
public class Demo5 { public static void main(String[] args) { //声明长度为500数组 boolean[] b = new boolean[500]; //将数组中的元素全部设置为true,表示初始状态人都在圈中 for(int i = 0;i<b.length;i++){ b[i] = true; } //计数器,统计当前报数的位置 int count = 0; //初始化圈中总人数 int len = b.length; //初始化数组的索引 int index = 0; //开始循环报数 while(len > 1){ //判断当前索引处的人是否在圈中 if(b[index]){ //报数 count++; //判断是否到达3的倍数 if(count == 3){ //计数器归零 count = 0; //剩余人数减一 len--; //将状态标记为离开(false) b[index] = false; } } //数组的索引递增 index++; //如果数完一圈,则索引归零 if(index == b.length){ index = 0; } } //遍历判断圈中剩余的最后一个为true的元素原来的索引即为剩下的人 for(int i = 0;i<b.length;i++){ if(b[i]){ System.out.println("剩余的人原来的位置:"+i); break; } } } }
java中的数组除了可定义基本类型数组,String类型数组外同时也能声明自定义类型数组,比如:
Student[] stus = new Student[5];
User[] users = {u1,u2,u3,u4...}
class Student{ int sno; String sname; String sex; public Student(int no,String name,String _sex){ sno = no; sname = name; sex = _sex; } } public class StudentTest{ public static void main(String[] args){ Student s1 = new Student(1,"张三疯","男"); Student s2 = new Student(2,"灭绝师太","女"); Student s3 = new Student(3,"张无忌","男"); //创建Student类型数组,存储多个Student对象 Student[] students = {s1,s2,s3} //动态初始化 Student[] students2 = new Student[3]; students2[0] = s1; students2[1] = s2; students2[2] = s3; } }
由于数组为定长数据结构,通常数组一旦定义,则长度无法修改,在实际使用中往往存在不够灵活的缺点;因此、在java中存在一种长度可变的动态数组:ArrayList,ArrayList类是基于可变长度数组的实现,底层实现原理依然是数组,内部通过数组拷贝的方式实现了数组的长度可变,ArrayList中提供了一系列用于操作数组中元素的方法,例如:
size() 获取ArrayList中元素的个数
add() 向ArrayList中添加新元素
get() 获取指定索引处的元素
remove() 删除指定索引处的元素
clear() 清除数组中的所有元素
...
示例代码:
public class ArrayListDemo { public static void main(String[] args) { //创建ArrayList对象 泛型(参数化类型) ArrayList<String> list = new ArrayList<String>(); list.add("helloworld"); list.add("jack"); list.add("rose"); //获取指定位置的元素 System.out.println(list.get(2)); //移除指定位置的元素 list.remove(1); //获取容器的长度(元素个数) System.out.println(list.size()); //清空集合(数组)中所有元素 list.clear(); } }
注意事项:
ArrayList若不指定泛型(参数化类型)时,可以向数组中添加任何数据类型;一旦指定泛型,则ArrayList内部的元素会限定只能存储与泛型一致的数据类型