//1.数组的声明
静态声明:预先知道数组中保存的每一个数据的时候
数据类型[] 变量名 = {类型值,类型值,……}。
int [] is = {1,2,3}; //Int一维数组
int[][] is2 = {{1,2,3},{2,3,5}……}; //int二维数组中有多个一维数组
动态声明:预先不知道数组中每个元素是什么的时候,使用动态声明
数据类型[] 变量名 = new 数据类型[长度];
int[] is = new int [10];
int [][] is2 = new int [5][2];//创建一个二维数组,二维数组中有5个一维数组,并且每个一维数组里有2个元素。
public class Test
{
public static void main(String[] args){
//静态声明
int [] arr = int {1,2,6,8};
int [] arr1 = new int[{1,2,3};
boolean [] bl = boolean {true,false};
//动态声明
int [] arr2 = new int [3];
}
}
//2.数组的使用
1.获取数据
public class Test
{
public static void main(String[] args){
int [] arr = {1,2,1,5};
System.out.println(arr.length);
System.out.println("第一个数据:" + arr[0] );
System.out.println("第四个数据:" + arr[3] );
}
}
2.设置数据
更改数据
语法:数组[下标] = 值;
arr [1] = 22;
3.遍历数据
public class Test
{
public static void main(String[] args){
int [] arr = new int[5];
for (int index = 0;index <arr.length ;index++ )//index 索引
{
System.out.println(arr[index]);
}
//把数组中每位元素的值,都赋值给这个变量
for(int element : arr){
System.out.println(element);
}
}
}
增强 for 循环
foreach:
for (元素数据类型 变量;数组)
{
}
4.常见异常
常见异常1:下标越界
System.out.println(arr[4]);
常见异常2:空指针异常
arr = null;
System.out.println(arr[2]);
//3.数组传递
int [] arr ={1,2,3,4};
//变量传递
m1(arr);
//字面量传递
m1(new int[]{1,2,3,4});
public class Test
{
public static void main(String[] args){
int[] arr ={1,2,3,4};
System.out.println(arr);
}
public static void m1(int [] arr){
for(int i : arr){
System.out.println(i);
}
}
}
//4.二维数组
传值:指的是 基本数据类型的传递。
传引用:指的是 引用类型的传递,引用类型保存地址,只能传递地址。
4.1数组的复制:
public class Test
{
public static void main(String[] args){
int[] src = {2,3,4,5,6};
int[] dest = {6,8,9,6};
// 原数组,原数组起始位置(包含),目标数组,目标数组起始位置。
//System.arraycopy(src,2,dest,3,3);
m1(src,2,dest,0,3);
for(int i: dest){
System.out.println(i);
}
}
public static void m1(int[] src,int srcPos,int[]dest,int destPos,int length){
for (int index = 0;index <length ;index++ )
{
dest[destPos++]= src[srcPos++];
}
}
}
4.2插入式复制
public class Test
{
public static void main(String[] args){
int []src = {1,3,5,6};
int []dest = {1,1,1,1};
int [] newDest = m1(src,2,dest,2,2);
for(int i:newDest){
System.out.println(i);
}
}
public static int[] m1(int[] src,int srcPos,int[]dest,int destPos,int length){
//新的数组长度
int[] newDest = new int[dest.length + length];
//数组dest中,起始位置之前的数据destPos,复制到新的数组中(newDest)
System.arraycopy(dest,0,newDest,0,destPos);
//把src中需要复制的复制到newDest的destPos位置
System.arraycopy(src, srcPos, newDest, destPos, length);
//把dest剩下的复制到newDest中,长度是newDest.length-length-destPos
System.arraycopy(dest, destPos, newDest, destPos+length, newDest.length-length-destPos);
return newDest;
}
}
声明
静态:
int[] [] arr = {
{1,2,3},
{1,6,8}
}
动态:
int[] [] arr = new int[5][3]; //二维数组中有5个一维,并且每个一维数组中有3个元素
int[] [] arr = new int[3][];// 二维数组中有3个一维数组,并且每个一维数组没有元素。
获取二维数组中的第一个一维数组
int[] arr_0 = arr[0];// {1,2,3}
int[] arr_00 = arr_0[0];// 1
遍历
for (int i = 0;i<arr.length ;i++ )
{
for (int j = 0;j<arr[i].length; j++)
{
System.out.print(arr[i][j]+ " ");
}
System.out.pritnln();
}
动态操作二维数组
int[][] arr = new int[5][5];
arr [2][3] = 1;
//杨辉三角
public class Test {
public static void main(String[] args) {
yangHui(7);
}
public static void yangHui(int n) {
// 规则 : 首尾为1,n行m列的值 = (n-1行m-1列) + (n-1行m列)
// 1 创建保存杨辉三角数据的二维数组(行数)
int[][] nums = new int[n][];
// 2 初始化每个一维数组中元素个数(列数)
for (int i = 0; i < nums.length; i++) {
nums[i] = new int[i + 1];
// 3 初始化首尾元素
nums[i][0] = 1;
nums[i][i] = 1;
// nums[i][i]=nums[i][0] = 1;
// 4 初始化非首尾元素
for (int j = 1; j < nums[i].length-1; j++) {
nums[i][j]= nums[i-1][j-1] + nums[i-1][j];
}
}
// 5 遍历
for (int[] is : nums) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
4.3交换变量的值
public class Test
{
public static void main (String[] args){
//1.借助中间变量
int x = 20;
int y =10;
int temp;;
temp = x;
x = y;
y = tmep;
//2.位移运算(面试常用)转换为二进制,每位异或,相同取0,不同取1
x = 1;
y = 2;
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
}
4.4 排序
让数据中数据按照某种规则进行排序,比如从大到小,或者从小到大。
//冒泡排序:
1.比较相邻的两个元素,如果第一个比第二个大,就交换位置(升序)
2.对每一对相邻的元素做同样的操作,比较完一轮之后,最后一个一定是最大。
3.重复上面的比较操作,每次都跳过最后一个不比较,所以比较次数一直递减。
public class Test
{
public static void main (String[] args){
int[] arr= {3,2,1,4};
maoPao(arr);
for(int i: arr){
System.out.println(i);
}
}
public static void maoPao(int[] arr){
for (int i = 0;i < arr.length-1;i++ )//外层循环规定比较多少轮
{
for (int j = 0; j < arr.length-1-i;j++ )//内层循环规定 每轮比较多少次
{
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
//选择排序
1.每次把最小的放到左边。
先拿出一个元素,假设他是最小的元素,然后挨个和后面所有的比较,比较完后如果比他小就换位。
2.循环比较,但是第一个不会再去进行比较。
public class Test
{
public static void main (String[] args){
int[] arr= {3,2,1,4};
maoPao(arr);
for(int i: arr){
System.out.println(i);
}
}
public static void maoPao(int[] arr){
for (int i = 0;i < arr.length ; i++)
{//假设当前元素就是最小的
int min = i;
for (int j = i+1;j < arr.length ;j++ )
{//比较大小
if(arr[min] > arr[j]){
//把比min小的值得下标,赋值给min
min = j;
}
}
if(min ! = i){
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
}
//API排序 只能升序
public class Test
{
public static void main (String[] args){
int[] arr= {3,2,1,4};
//使用API
Array.sort(arr);
for(int i: arr){
System.out.println(i);
}
}
}
//传统查询
public class Test
{
public static void main(String[] args){
}
public static int search(int[]arr,int target){
for (int i = 0; i < arr.length ; i++)
{
if (arr[i] == target)
{
return 1;
}
}
return -1;
}
}
//二分法查找:
1.必须建立在已排序的基础之上。
2.一般没有重复数据,如果有,只能是找到谁算谁。
3.一般用于查找固定有序的数据,因为二分法
public class Test
{
public static void main(String[] args){
}
public static int erFen(int[]arr,int target{
//1.确定起始索引,结束索引,中间索引。
int startIndex = 0;
int endIndex = arr.length-1;
int m = (startIndex + endIndex)/2
//2.使用目标数据和中间索引的数值比较。
//终止条件:不存在该数据,起始值大于结束值
while(startIndex <= endIndex){
//3.第一种:中间值就是目标值,直接返回中间索引。
if ( arr[m] = target)
{
return m;
}
//4.第二种:中间值小于目标值,结束值不变,起始值=中间值+1
if (arr[m] < target)
{
startIndex = m+1;
}
//5.第三种:中间值大于目标值,起始值不变,结束值=中间值-1
if (arr[m] > target)
{
endIndex = m-1;
}
//重新生成中间值
m = (startIndex + endIndex)/2;
}
return -1;
}
}