一、数组的排序
1、冒泡排序
1.口诀:
N个数字来排队
两辆相比小靠前
外层循环N-1
内层循环N-1-i
2.算法
public class Test01{
public static void main(String[] args){
int[] nums = {81,23,56,18,33,9};
for(int i = 0;i<nums.length-1;i++){
for(int j = 0;j<nums.length-1-i;j++){
if(nums[j] > nums[j+1]){
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
for(int element : nums){
System.out.println(element);
}
}
}
2、线性查找/顺序查找
public class Test02{
public static void main(String[] args){
int[] nums = {81,23,56,18,33,9};
int num = 18;//需要查找的值
for(int i = 0;i<nums.length;i++){
if(nums[i] == num){
System.out.println("找到元素了,下标为:" + i);
break;
}
}
}
}
3、二叉查找/二分法查找
注意:必须先排序
import java.util.Arrays;
public class Test03{
public static void main(String[] args){
int[] nums = {81,23,56,18,33,9};
int num = 18;//需要查找的值
//数组的排序 -- 9,18,23,33,56,81
Arrays.sort(nums);
int start = 0;
int end = nums.length-1;
while(start <= end){
int mid = (start+end)/2;
if(nums[mid] > num){
end = mid-1;
}else if(nums[mid] < num){
start = mid+1;
}else{
System.out.println("找到元素了,下标为:" + mid);
break;
}
}
}
}
二、数组的复制
1、复制的第一种方式
缺点:修改源数组,新数组也会发生改变
public class Test04{
public static void main(String[] args){
//源数组
String[] names = {"小明","小米","小欢","小华"};
//新数组
String[] newNames = names;
//修改源数组的数据
names[1] = "小苏";
//遍历新数组
for(String str : newNames){
System.out.println(str);
}
}
}
2、复制的第二种方式
public class Test05{
public static void main(String[] args){
//源数组
String[] names = {"小明","小米","小欢","小华"};
//新数组
String[] newNames = new String[names.length];
//数据的复制
for(int i = 0;i<names.length;i++){
newNames[i] = names[i];
}
//修改源数组的数据
names[1] = "小苏";
//遍历新数组
for(String str : newNames){
System.out.println(str);
}
}
}
3、数组的扩容
public class Test06{
/**
知识点:数组的扩容
扩容机制:原来长度的1.5
*/
public static void main(String[] args){
//源数组
String[] names = {"小明","小米","小欢","小华"};
//老的长度
int oldCapacity = names.length;
//新的长度
int newCapacity = oldCapacity + (oldCapacity >> 1);
//创建新数组
String[] newNames = new String[newCapacity];
//将源数组的数据复制到新数组中
for(int i = 0;i<names.length;i++){
newNames[i] = names[i];
}
//将新数组的地址复制给源数组
names = newNames;
//遍历源数组
for(String str : names){
System.out.println(str);
}
}
}
三、数组的删除
1、数组删除方式1
缺点:数组是为了存储数据而创建,这种删除元素的方式会把数组变得越来越小
public class Test07{
public static void main(String[] args){
//源数组
String[] names = {"小明","小米","小欢","小华"};//0,1,2,3
//创建新数组
String[] newNames = new String[names.length-1];//0,1,2
//遍历源数组,将除了"小米"的元素都放入新数组中
int index = 0;
for(String element : names){
if(!element.equals("小米")){
newNames[index++] = element;
}
}
//将新数组的引用复制给原数组
names = newNames;
//遍历源数组
for(String element : names){
System.out.println(element);
}
}
}
2、数组删除方式2
public class Test08{
public static void main(String[] args){
//源数组
String[] names = {"小明","小米","小欢","小华"};//0,1,2,3
//数据的迁移
for(int i = 1;i<names.length-1;i++){
names[i] = names[i+1];
}
//把最后一个元素置为null
names[names.length-1] = null;
//遍历源数组
for(String element : names){
System.out.println(el四ement);
}
}
}
四、可变参数
1.含义:可变参数本质意义上就是数组
2.需求: 设计一个方法,传入两个int值,返回最大值
需求升级:设计一个方法,传入三个int值,返回最大值
需求升级:设计一个方法,传入四个int值,返回最大值
需求升级:设计一个方法,传入n个int值,返回最大值
public class Test10{
public static void main(String[] args){
//1,2,3,4,5,6,7,8,9,10 -- 作为数组的元素,添加到数组中
int max = getMax(1,2,3,4,5,6,7,8,9,10);
System.out.println("最大值为:" + max);
}
//int... nums -- 就是数组
public static int getMax(int... nums){
if(nums.length == 0){//说明数组中没有元素
return -1;
}
int max = nums[0];
for(int i = 1;i<nums.length;i++){
if(max < nums[i]){
max = nums[i];
}
}
return max;
}
//注意:可变参数后面不能跟其他的参数
public static void method(int i,String... ss){}
}
五、Java中Arrays工具类
1.含义:Java给我们提供的专门操作数组的工具类
2.工具类的概念:该类中所有的方法都是静态的,直接使用类名调用
API的概念:Java提供类的说明书(放在桌面去,方便我们查询)
import java.util.Arrays;
public class Test11{
public static void main(String[] args){
int[] nums = {81,23,56,18,33,9};
//排序 -- 9,18,23,33,56,81
Arrays.sort(nums);
//查找
//返回值:搜索键的索引,如果它包含在数组中;否则, (-(insertion point) - 1)
//返回值:如果查找的值在数组中,就返回下标;否则,返回(-插入点-1)
int index = Arrays.binarySearch(nums,40);
System.out.println("查找元素的下标为:" + index);
//拷贝数组(目标数组,新的长度)
int[] arr1 = Arrays.copyOf(nums,nums.length*2);
//拷贝数组(目标数组,开始下标-包含,结束下标-不包含)
int[] arr2 = Arrays.copyOfRange(arr1,2,8);
//替换(目标数组,要替换的值)
Arrays.fill(arr2,888);
//替换(目标数组,开始下标-包含,结束下标-不包含,要替换的值)
Arrays.fill(arr2,1,4,666);
//将数组转换为字符串 -- [23,33,56,81,0,0]
System.out.println(Arrays.toString(arr2));
}
}
六、二维数组
1.含义:包含了多个一维数组
2.数组的声明:数据类型 数组名;
public class Test12{
public static void main(String[] args){
//静态初始化一
//String[][] names = new String[][]{{"小明","小米","小花"},{"小天","小田","小红","小绿"}};
//静态初始化二(先声明,再初始化)
//String[][] names;
//names = new String[][]{{"小明","小米","小花"},{"小天","小田","小红","小绿"}};
//静态初始化三(简化写法一)
String[][] names = {{"小明","小米","小花"},{"小天","小田","小红","小绿"}};
//设置指定下标上的元素
names[1][1] = "小苏";
//获取指定下标上的元素
String str = names[1][1];
System.out.println("获取指定下标上的元素:" + str);
//获取长度
System.out.println("获取二维数组中一维数组的个数:" + names.length);//2
System.out.println("获取二维数组中第一个一维数组元素的个数:" + names[0].length);//3
System.out.println("获取二维数组中第二个一维数组元素的个数:" + names[1].length);//4
System.out.println("-----------");
//遍历思路:先循环取出二维数组中的一维数组,再循环取出一维数组中的元素
//遍历数组 -- for
for(int i = 0;i<names.length;i++){
for(int j = 0;j<names[i].length;j++){
System.out.println(names[i][j]);
}
}
System.out.println("-----------");
//遍历数组 -- foreach
for(String[] ss : names){
for(String element : ss){
System.out.println(element);
}
}
}
}