1、遍历数组
class ArrayDemo
{
public static void main(String[] args)
{
int[] arr = {10,21,18,12,22};
System.out.println("数组中元素个数:"+arr.length);
for (int i=0;i<arr.length ;i++ )
{
System.out.println("arr["+i+"]="+arr[i]);
}
}
}
2、数组求和
class ArrayAdd
{
public static void main(String[] args)
{
int[] arr = {23,11,25,88,45,22,88};
int sum = add(arr);
System.out.println("sum="+sum);
}
public static int add(int[] arr)
{
//1,定义变量记录和。
int sum = 0;
//2,通过循环对数组进行遍历。
for(int x=0; x<arr.length; x++)
{
sum = sum + arr[x];
}
return sum;
}
}
3、数组求最值
class ArrayMax
{
public static void main(String[] args)
{
int[] arr = {23,55,55,45,90,11};
int max = getMax(arr);
System.out.println("max="+max);
}
public static int getMax(int[] arr)
{
//1,定义变量。初始化为数组中的任意一个元素。
int max = arr[0];
//2,遍历数组。
for(int x=1; x<arr.length; x++)
{
//遍历的元素只要比max大,就用max记录下来。遍历到最后max中记录就是最大值。
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])
max = x;
}
return arr[max];
}
}
4、数组转字符串
class ArrayToString
{
public static void main(String[] args)
{
int[] arr = {22,22,13,85,15};
String str = toString(arr);
System.out.println(str);
}
public static String toString(int[] arr)
{
//1,定义字符串变量。
String temp = "[";
//2,遍历数组。将每一个数组的元素和字符串相连接。
for(int x = 0; x < arr.length; x++)
{
//3,判断,不是最后一个元素,后面连接逗号,是最后一个元素,后面不连接逗号。
if(x!=arr.length-1)
temp = temp + arr[x] + ",";
else
temp = temp + arr[x] + "]";
}
//4、将连接后的字符串返回。
return temp;
}
}
5、数组反转
class ArrayReverse
{
public static void main(String[] args)
{
int[] arr = {13,22,8,7,15};
printArray(arr);
reverseArray(arr);
printArray(arr);
}
//1、定义功能,接受数组
public static void reverseArray(int[] arr)
{
//2、遍历数组,每遍历一次交换数组两个位置的上的值,
// start代表头角标,end代表尾角标
for (int start = 0,end=arr.length-1;start<end ;start++,end-- )
{
//3、交换每次遍历的两个空间中的数据
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
//此功能为打印数组
public static void printArray(int[] arr)
{
for (int i=0;i<arr.length ;i++ )
{
if(i!=arr.length-1)
System.out.print(arr[i]+",");
else
System.out.println(arr[i]);
}
}
}
6、十进制-->十六进制
class ArrayToHex
{
public static void main(String[] args)
{
int num = 100;
String str_hex = toHex(num);
System.out.println(str_hex);
}
public static String toHex(int num)
{
//1,定义容器。存储的是字符,长度为8.一个整数最多8个16进制位。
char[] chs = new char[8];
//2,定义一个用于操作数组的索引。
int index = chs.length;
while(num!=0)
{
int temp = num & 15;
if(temp > 9)
chs[--index] = ((char)(temp-10+'A'));
else
chs[--index] = ((char)(temp+'0'));
num = num >>> 4;
}
System.out.println("index="+index);
return "0x"+toString(chs,index);
}
//定义一个功能,将字符数组转成字符串。
public static String toString(char[] arr,int index)
{
String temp = "";
for(int x=index; x<arr.length; x++)
{
temp = temp + arr[x];
}
return temp;
}
}
另一种方法(查表法):
class ArrayToHex
{
public static void main(String[] args)
{
int num = 100;
String str_hex = toHex(num);
System.out.println("hex:"+str_hex);
}
public static String toHex(int num)
{
//1,建立表。
char[] chs = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//2,创建临时容器。
char[] arr = new char[8];
//3,创建操作临时容器的角标。
int index = arr.length;
//4,通过循环对num进行& >>等运算。
while(num!=0)
{
//5,对num进行&运算。
int temp = num & 15;
//6,根据&运算后的结果作为角标查表,获取对应的字符。并将字符存储到临时容器中。
arr[--index] = chs[temp];
//7,对num进行右移。
num = num >>> 4;
}
return "0x"+toString(arr,index);
}
//定义一个功能,将字符数组转成字符串。
public static String toString(char[] arr,int index)
{
String temp = "";
for(int x=index; x<arr.length; x++)
{
temp = temp + arr[x];
}
return temp;
}
}
7、数组排序:
7.1选择排序
public static void selectSort(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;
}
}
}
}
7.2冒泡排序
public static void bubbleSort(int[] arr)
{
for(int x=0; x<arr.length-1; x++)
{
for(int y=0; y<arr.length-1-x; y++)
{
if(arr[y]>arr[y+1])
{
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
8、数组查找
8.1一般查找
public static int searchKey(int[] arr,int key)
{
//遍历查找。
for(int x=0; x<arr.length; x++)
{
if(arr[x]==key)
return x;
}
return -1;//-1,代表的是角标不存在的情况。
}
8.2折半查找
public static int binarySearch(int[] arr,int key)
{
//1,定义三个变量,记录头角标,尾角标,中间角标。
int max,min,mid;
min = 0;
max = arr.length-1;
mid = (max+min)>>1;
while(arr[mid]!=key)
{
if(key>arr[mid])
min = mid + 1;
else if(key<arr[mid])
max = mid - 1;
//判断元素是否存在。
if(max<min)
return -1;
mid = (max+min)>>1;
}
return mid;
}