使用二维数组打印一个10行的杨辉三角
public class YangHuiTest {
public static void main(String[] args) {
//1.声明并初始化数组
int[][] yangHui= new int[10][];
//2.给数组赋值
for(int i = 0;i < yangHui.length;i++){
yangHui[i] = new int[i + 1];
//给数组首末元素赋值
yangHui[i][0] = yangHui[i][i] = 1;
//给非首末元素赋值
if(i > 1) {
//可写可不写
for(int j = 1;j < yangHui[i].length - 1;j++) {
yangHui[i][j] = yangHui[i-1][j-1] + yangHui[i-1][j];
}
}
}
//3.遍历
for(int i = 0;i < yangHui.length;i++) {
for(int j = 0;j < yangHui[i].length;j++) {
System.out.print(yangHui[i][j] + " ");
}
System.out.println();
}
}
}
定义一个int类型的十个元素的数组,元素为随即的俩位数,求数组的最大值和最小值和总和和平均值
随机数的公式为 [a,b]
(int)(math.random() * (b - 1 + 1) + a);
public class ArrayTest {
public static void main(String[] args) {
int[] arr = new int[10];
//数组赋值
for(int i = 0;i < arr.length;i++) {
arr[i] = (int)(Math.random() * (99 - 10 + 1) + 10);
}
//遍历
for(int i = 0;i < arr.length;i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
//最大值
int max = arr[0];
for(int i = 0;i < arr.length;i++) {
if(max < arr[i]) {
max = arr[i];
}
}
System.out.println("数组最大值为" + max);
//最小值
int min = arr[0];
for(int i = 0;i < arr.length;i++) {
if(min > arr[i]) {
min = arr[i];
}
}
System.out.println("数组最小值" + min);
//总和
int sum = 0;
for(int i = 0;i < arr.length;i++) {
sum += arr[i];
}
System.out.println("数组的总和为:" + sum);
//平均数
int ageValue = sum / arr.length;
System.out.println("平均数为:" + ageValue);
}
}
数组的复制、反转、线性查找、二分法查找
public class ArrayTest2 {
public static void main(String[] args) {
String[] arr = new String[] {
"gg","ff","ww","aa","qq","bb"};
//数组的复制(区别于数组的赋值:arr1 = arr)
String[] arr1 = new String[arr.length];
for(int i = 0;i < arr1.length;i++) {
arr1[i] = arr[i];
}
for(int i = 0;i < arr1.length;i++) {
System.out.print(arr1[i]);
}
System.out.println();
//数组的反转
for(int i = 0;i < arr1.length / 2;i++){
String temp = arr1[i];
arr1[i] = arr1[arr1.length - i - 1];
arr1[arr1.length - i - 1] = temp;
}
for(int i = 0;i < arr1.length;i++) {
System.out.print(arr1[i]);
}
//线性查找
String dest = "ss";
boolean isFlag = true;
for(int i = 0;i < arr.length;i++) {
if(dest.equals(arr[i])) {
System.out.println("找到了指定的元素,位置为:" + i);
isFlag = false;
break;
}
}
if(isFlag) {
System.out.println("很遗憾,没有找到!");
}
//二分法查找 :前提 所要查找的数组必须有序
int[] arr2 = new int[] {
-45,-33,-20,-10,0,5,20,56,200,435};
int dest1 = 5;
int head = 0;
int end = arr2.length - 1;
boolean isFlag1 = true;
while(head <= end) {
int mid = (head + end) / 2;
if(dest1 == arr2[mid]) {
System.out.println("恭喜你,找到了,位置为:" + mid);
isFlag1 = false;
break;
}else if(dest1 < arr2[mid]) {
end = mid - 1;
}else{
head = head + 1;
}
}
if(isFlag1) {
System.out.println("没找到");
}
}
}
数组的冒泡排序:从大到小排序
public class BubbleSoreTest {
public static void main(String[] args) {
int[] arr = new int[] {
-34,-78,-123,-3,0,4,2,65,320,78,96};
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;
}
}
}
for(int i = 0;i < arr.length;i++) {
System.out.print(arr[i] + " ");
}
}
}
java.util.Arrays:操作数组的工具类,里面定义了很多操作数组的方法
import java.util.Arrays;
public class ArraysTest {
public static void main(String[] args) {
//判断俩个数组是否相等 boolean equals (int[] a,int[] b);
int[] arr1 = new int[]{
1,2,3,4};
int[] arr2 = new int[]{
1,3,2,4};
boolean isEquals = Arrays.equals(arr1, arr2);
System.out.println(isEquals);
//输出数组信息 String toString(int[] a)
System.out.println(Arrays.toString(arr1));
//将指定值填充到数组中 void fill(int[] a,int val)
Arrays.fill(arr1, 5);
System.out.println(Arrays.toString(arr1));
//对数组进行排序 void sort(int[] a)
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));
//二分查找 int binarySearch(int[] a,int key)
int[] arr3 = new int[] {
-45,-33,-20,-10,0,5,20,56,200,435};
int index = Arrays.binarySearch(arr3, 10);
if(index < 0) {
System.out.println("没找到");
}else {
System.out.println(index);
}
}
}
数组中的常见异常
1.数组角标越界的异常ArrayIndexOutOfBoundsException
2.空指针异常
NullPointerException
public class ArraysExceptionTest {
public static void main(String[] args) {
/*
1.数组角标越界的异常 ArrayIndexOutOfBoundsException
int[] arr = new int[] {1,2,3,4,5};
for(int i =0;i <= arr.length;i++) {
System.out.println(arr[i]);
}
System.out.println(arr[-2]);
2.NullPointerException 空指针
情况一:
int[] arr1 = new int[] {1,2,3};
arr1 = null;
System.out.println(arr1[0]);
情况二:
int[][] arr2 = new int[4][];
System.out.println(arr2[0][0]);
情况三:
String arr3 = new String[]{"AA","BB","CC"};
arr3[0] = null;
System.out.println(arr3[0].toString());
*/
}
}