1.数组静态初始化:
ArrayIndexOutOfBoundsException:角标越界异常:操作数组是时访问到了数组中不存在的角标。
class Shuzu1
{
public static void main(String[] args)
{
int[] arr1 = new int[3];
System.out.println("arr1[3]="+arr1[3]);
}
}
NullPointerException:空指正异常:当引用没有任何指向,值为空的时候,该引用还在操作实体。
class Shuzu1
{
public static void main(String[] args)
{
int[] arr1 = new int[3];
arr1 = null;
System.out.println("arr1[3]="+arr1[3]);
}
}
Byte、 short、int 、long默认初值化值为 0;double默认为 0.0 、float0.0默认为 0.0f;boolean 默认为false;对象类型的数组默认为 null。
2.数组的操作(遍历):
获取数组中的元素,获取数组长度,数组中的元素求和。
class Shuzu1
{
public static void main(String[] args)
{
int[] arr2 = new int[]{1,2,3,4,5,6};
System.out.println("arr.length="+arr2.length);
// 数组名 .length:获取数组中元素的个数。
//int sum=0;
for(int x=0;x<arr2.length;x++)
{
//Sum+=arr[x];
System.out.println("arr2["+x+"]"+"="+arr2[x]);
}
}
}
定义一个功能:打印数组中的元素,元素之间用“,”隔开:
class Shuzu1
{
public static void main(String[] args)
{
int[] arr2 = new int[]{1,2,3,4,5,6};
//System.out.println(arr2); 打印结果为: [I@1fb8ee3:数组, 数据类型, @地址( 哈希算法 )
Printarr(arr2);
}
public static void Printarr(int[] arr2)
{
for(int x=0;x<arr2.length;x++)
{
if(x!=arr2.length-1)
System.out.print(arr2[x]+",");
else
System.out.print(arr2[x]);
}
}
}
3.数组的操作(获取最值):
/*
需求:获取数组中的最值 (适合于数值型的数组,boolean值只有 false跟true ,没有最值 )
思路:定义一个变量,初始化为数组中的任意一个元素,遍历数组中的元素,让每个元素都跟变量比较,获取较大 /小的值,赋给变量,最终可获取到最值。
*/
class Shuzu2
{
public static void main(String[] args)
{
int[] arr = new int[]{1,2,3,4,5,6};
//System.out.println(getMax(arr));
System.out.println(getMax_2(arr));
}
/*public static int getMax(int[] arr)
{
int Max = arr[0];
for(int x=0;x<6;x++)
{
if(arr[x]>Max)
Max = arr[x];
}
return Max;
}
*/
public static int getMax_2(int[] arr)
{
int Max = 0;
for(int x=1;x<arr.length;x++)
{
if(arr[x]>arr[Max])
arr[Max]=arr[x];
}
return arr[Max];
}
}
//获取最小值只需要将“ >”修改为“< ”即可。
4.选择排序:
内循环结束一次,最值出现在头角标位置上;
class Selectsort1
{
public static void main(String[] args)
{
int[] arr = new int[]{1,2,43,4,56,6,7};
printarr(arr);// 排序前打印
System.out.println();
sort(arr);
printarr(arr);// 排序后打印;
//System.out.println(arr);
}
public static void sort(int[] arr)
{
for(int x=0;x<arr.length-1;x++)
{
for(int y=x+1;y<arr.length;y++)
{
if(arr[x]>arr[y])
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
public static void printarr(int[] arr)
{
for(int x=0;x<arr.length;x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+",");
else
System.out.print(arr[x]);
}
}
}
5.冒泡排序:
class bubblesort
{
public static void main(String[] args)
{
int[] arr = new int[]{2,3,45,6,7,87,8,89};
printarr(arr);
bubblesort(arr);
System.out.println();
printarr(arr);
}
public static void printarr(int[] arr)
{
for(int x=0;x<arr.length;x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+",");
else
System.out.print(arr[x]);
}
}
public static void bubblesort(int[] arr)
{
for(int x=0;x<arr.length-1;x++)//-x:让每次比较的次数减少,-1:防止角标越界
{
for(int y=x+1;y<arr.length-x-1;y++)
if(arr[y]>arr[y+1])
{
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
}
Java高级排序功能:
import java.util.*;
class paixu
{
public static void main(String[] args)
{
int[] arr = new int[]{4,6,7,8,3,2,5,55};
Arrays.sort(arr);
printarr(arr);
}
public static void printarr(int[] arr)
{
for(int x=0;x<arr.length;x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+",");
else
System.out.print(arr[x]+",");
}
}
}
6.位置置换功能:
class bubblesort
{
public static void main(String[] args)
{
int[] arr = new int[]{2,3,45,6,7,87,8,89};
printarr(arr);
bubblesort(arr);
System.out.println();
printarr(arr);
}
public static void printarr(int[] arr)
{
for(int x=0;x<arr.length;x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+",");
else
System.out.print(arr[x]);
}
}
public static void bubblesort(int[] arr)
{
for(int x=0;x<arr.length-1;x++)
{
for(int y=x+1;y<arr.length-x-1;y++)
{
if(arr[y]>arr[y+1])
{
/*int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;*/
swap(arr,y,y+1); }
}
}
}
public static void swap(int[] arr,int a,int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
7.折半查找:
//位运算:
<< 结果为x乘以 2的移动次幂。
>> 结果为x除以 2的移动次幂。
条件:数组是有序的数组;
class halfsearch
{
public static void main(String[] args)
{
int[] arr = new int[]{1,3,5,6,7,18,28};
//int x = halfsearch(arr,0);
int x = halfsearch_2(arr,0);
System.out.println(x);
}
/*
public static int halfsearch(int[] arr,int key)
{
int min=0;
int max=arr.length-1;
int mid=(max+min)>>1;
while(key!=arr[mid])
{
if(key>arr[mid])
min=mid+1;
else if(key<arr[mid])
max=mid-1;
mid=(max+min)/2;
if(min>max)
return -1;
}
return mid;
}
*/
public static int halfsearch_2(int[] arr,int key)
{
int min=0;
int max=arr.length-1;
int mid=(max+min)>>1;
while(min<=max)
{
mid = (min+max)/2;
if(key>arr[mid])
min=mid+1;
else if(key<arr[mid])
max=mid-1;
else
return mid;
}
return -1;
}
}
//把一个数插入到一个有序的数组中,计算这个数在数组中的位置;
将上个程序中return -1;换成 return
mid;即可。
8.十进制转二进制:
十进制转二进制就是%2, /2,结果从右向左排列。
class Cece4
{
public static void main(String[] args)
{
toBin(6);
}
public static void toBin(int num)
{
StringBuffer s = new StringBuffer();
while(num>0)
{
s.append(num%2);
num = num/2;
}
System.out.print(s.reverse());
}
}
9.十进制转换十六进制
class Cece5
{
public static void main(String[] args)
{
toBin(6);
}
public static void toBin(int num)
{
StringBuffer sb = new StringBuffer();
for(int x=0;x<8;x++)
{
int temp = (num & 15);
if(temp>9)
//System.out.print((char)(temp-10+'A'));
sb.append((char)(temp-10+'A'));
else
//System.out.print(temp);
sb.append(temp);
num = num>>>4;
}
System.out.print(sb.reverse());
}
}
10.查表法:十进制转十六进制
class Cece6
{
public static void main(String[] args)
{
chabiao(-600);
System.out.println();
chabiao(60);
}
public static void chabiao(int num)
{
char[] arr1 = new char[]{'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};
char[] arr2 = new char[8];
//int pos = 0;
int pos = arr2.length;
while(num!=0)
//for(int x=0;x<8;x++)
{
int temp = num & 15;
//System.out.println(arr1[temp]);
//arr2[pos++] = arr1[temp];
arr2[--pos] = arr1[temp];
num = num>>>4;
}
//for(int x=pos-1;x>=0;x--)
for(int x=pos;x<arr2.length;x++)
{
System.out.print(arr2[x]);
}
}
}
11.查表法:十进制转成二进制
class Cece7
{
public static void main(String[] args)
{
toBin(6);
System.out.println();
toBin(-6);
}
public static void toBin(int num)
{
char[] arr1 = new char[]{'0','1'};
char[] arr2 = new char[32];
int pos = arr2.length;
while(num!=0)
{
int temp = num & 1;
arr2[--pos] = arr1[temp];
num = num >>> 1;
}
for(int x=pos;x<arr2.length;x++)
{
System.out.print(arr2[x]);
}
}
}
12.进制优化:
class Cece8
{
public static void main(String[] args)
{
toEr(6);
toEr(-6);
}
public static void toEr(int num)
{
trans(num,1,1);
}
public static void toBa(int num)
{
trans(num,7,3);
}
public static void to16(int num)
{
trans(num,15,4);
}
public static void trans(int num,int base,int offset)
{
char[] arr1 = new char[]{'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};
char[] arr2 = new char[32];
int pos = arr2.length;
while(num!=0)
{
int temp = num & base;
arr2[--pos] = arr1[temp];
num = num >>> offset;
}
for(int x=pos;x<arr2.length;x++)
{
System.out.print(arr2[x]);
}
}
}
13.二维数组:
格式1:int[][] arr = new [3][2];
定义了一个arr 的二维数组,其中有三个一维数组,每个一维数组中元素的个数为 2,一维数组的名称分别为 arr[0]、arr[1] 、arr[2],给第一个数组中的一角标赋值 70的写法为arr[0][1]
= 70;
System.out.println(arr);//[[I@7889
System.out.println(arr[0]);//[I@890bc
System.out.println(arr[0][1]);//0
格式2: int[][]
arr = new[3][];
定义了一个arr 的二维数组,其中有三个一维数组,每个一维数组对应的都是默认初始化值 null,可以对这三个数组分别进行初始化赋值: arr[0]
= new int[3];arr[1] = new int[2];arr[2] = new int[1];
System.out.println(arr);//[[I@7889
System.out.println(arr[0]);//null
System.out.println(arr.length);//打印二维数组的长度
System.out.println(arr[20].length);//打印二维数组中第一个一维数组的长度
int[][] arr = {{},{},{}};
//求二维数组中所有元素的和
class Cece9
{
public static void main(String[] args)
{
int[][] arr = {{1,3,4,7},{5,6,8,9},{11,32,44}};
int sum = 0;
for(int x=0;x<arr.length;x++)// 遍历二位数组。
{
for(int y=0;y<arr[x].length;y++)
{
sum = sum + arr[x][y];
}
}
System.out.println("sum="+sum);
}
}
14.二维数组小练习:
int[] x; int x[];
int[][] y; int y[][]; int[] y[];
int[] x,y[];//x一维, y二维。
int[] x;
int[] y[];
a.
x[0] = y;//error
b.
y[0] = x;//yes
c.
y[0][0] = x;//error
d.
x[0][0] = y;//error
e.
y[0][0] = x[0];//yes
f.
x=y;//error