数组可以存放多个类型相同的数据,是引用类型。
public class ArrayDefine{
public static void main(String[] args){
//第一种定义方式,定义时初始化
int intArr[] = new int[10];
String[] stringArr = new String[5];
for(int i = 0; i < intArr.length; i++){
intArr[i] = i;
System.out.print(intArr[i] + " ");
}
System.out.println();
String str = "ABCDE";
for(int i = 0; i < stringArr.length; i++){
stringArr[i] = "student" + str.charAt(i);
System.out.print(stringArr[i] + " ");
}
System.out.println();
//第二种方式,先定义再初始化
double doubleArr[];
doubleArr = new double[5];
boolean booleanArr[];
booleanArr = new boolean[5];
for(int i = 0; i < doubleArr.length; i++){
doubleArr[i] = (double)i / 5;
System.out.print(doubleArr[i] + " ");
}
System.out.println();
for(int i = 0; i < booleanArr.length; i++){
booleanArr[i] = (i % 2 == 0);
System.out.print(booleanArr[i] + " ");
}
System.out.println();
//第三种方式,静态初始化,只能在定义的时候
Object[] objectArr = {new String("abcde"), new Boolean(true), new Integer(109)};
for(int i = 0; i < objectArr.length; i++){
System.out.print(objectArr[i] + " ");
}
}
}
基本数据类型在赋值时,传递的是值。数组是引用类型,在使用赋值运算符时,传递的是引用地址。
public class ArrayAssignment{
public static void main(String[] args){
int[] intArr = {1,2,3,4,5};
//赋值传递的是引用地址
//也就是说现在两个变量指向统一堆空间
//对其中一个操作会影响到另一个
int[] intArr2 = intArr;
for(int i = 0; i < 5; i++){
System.out.print(intArr[i] + " ");
}
System.out.println();
for(int i = 0; i < 5; i++){
System.out.print(intArr2[i] + " ");
}
System.out.println();
intArr2[2] = 123;
for(int i = 0; i < 5; i++){
System.out.print(intArr[i] + " ");
}
System.out.println();
}
}
public class ArrayCopy{
public static void main(String[] args){
//
int[] intArr1 = {1,2,3,4,5,6,7,8,9,0};
int[] intArr2 = new int[intArr1.length];
myPrint(intArr1);
myPrint(intArr2);
//拷贝数组元素
for(int i = 0; i < intArr1.length; i++){
intArr2[i] = intArr1[i];
}
myPrint(intArr1);
myPrint(intArr2);
}
public static void myPrint(int[] arr){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
public class ArrayReverse{
public static void main(String[] args){
int[] intArr = {1,2,3,4,5,6,7,8,9,0};
//使用额外空间反转
int[] temp = new int[intArr.length];
for(int i = 0; i < intArr.length; i++){
temp[temp.length - i - 1] = intArr[i];
}
for(int i = 0; i < intArr.length; i++){
intArr[i] = temp[i];
}
myPrint(intArr);
//不使用额外空间,直接在原数组上反转
int left = 0, right = intArr.length - 1;
while(true){
if(left < right){
int n = intArr[left];
intArr[left] = intArr[right];
intArr[right] = n;
left++;
right--;
}else{
break;
}
}
myPrint(intArr);
}
public static void myPrint(Object arr){
for(int i = 0; i < ((int[])arr).length; i++){
System.out.print(((int[])arr)[i] + " ");
}
System.out.println();
}
}
public class ArrayExtend{
public static void main(String[] args){
int[] intArr = {};
System.out.println("数组的长度是:" + intArr.length);
//数组扩容需要重新创建一个数组,然后使引用指向新的数组
for(int i = 0; i < 100; i++){
if(intArr.length <= i){
//为数组扩容
int[] temp = new int[intArr.length + 20];
//先要拷贝元素
for(int j = 0; j < intArr.length; j++){
temp[j] = intArr[j];
}
intArr = temp;
System.out.println("数组扩容了,现在的容量是" + intArr.length);
}
intArr[i] = i;
}
for(int i = 0; i < intArr.length; i++){
System.out.print(intArr[i] + " ");
}
System.out.println();
}
}
public class BubbleSort{
public static void main(String[] args){
int[] arr = {53,18,93,66,43,88,99,23,22,64};
//冒泡排序,从小到大排
boolean flag = true;
for(int p = 1; p < arr.length; p++){
flag = true;
for(int i = 0; i < arr.length - p; i++){
if(arr[i] > arr[i+1]){
arr[i] = arr[i] ^ arr[i+1];
arr[i+1] = arr[i] ^ arr[i+1];
arr[i] = arr[i] ^ arr[i+1];
flag = false;
}
}
if(flag){
break;
}
}
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
public class Search{
public static void main(String[] args){
int[] arr = {84, 86, 23, 31, 22 ,61 ,26, 34, 27, 71};
//顺序查找31所在下标
int index = seqSearch(arr);
System.out.println("顺序查找的下标为:" + index);
//二分查找
index = binarySearch(arr);
System.out.println("二分查找的下标为:" + index);
}
public static int seqSearch(int[] arr){
for(int i = 0; i < arr.length; i++){
if(arr[i] == 31){
return i;
}
}
return -1;
}
public static int binarySearch(int[] arr){
int left = 0, right = arr.length;
int center;
while(true){
if(left > right){
break;
}
center = (left + right) / 2;
if(arr[center] == 31){
return center;
}else if(arr[center] < 31){
left = center + 1;
}else{
right = center - 1;
}
}
return -1;
}
}
arr[i][j]
:表示的是第 i 行,第 j 列的元素,也就是第 i+1 个一维数组的第 j+1 个元素。public class TwoDimenArray{
public static void main(String[] args){
int[][] arr = {{1,2,3},{4,5,6},{7,8}};
for(int i = 0; i < arr.length; i++){
System.out.println("本行的长度为:"+arr[i].length);
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
public class TwoDimenArrayInit{
public static void main(String[] args){
//方式一:直接new
int[][] arr1 = new int[4][5];
assign(arr1);
printArr(arr1);
//方式二:先声明,再初始化
int[][] arr2;
arr2 = new int[7][8];
assign(arr2);
printArr(arr2);
//方式三:列数不确定,先指定数组的行数,再动态分配一维数组的空间
int[][] arr3 = new int[5][];
//赋值
for(int i = 0; i < arr3.length; i++){
arr3[i] = new int[i+1];
for(int j = 0; j < arr3[i].length; j++){
arr3[i][j] = i + 1;
}
}
//打印数组
printArr(arr3);
//方式四:静态初始化
int[][] arr4 = {{1,2,3,4,5},{1,2,3,4},{1,2,3},{1,2},{1}};
printArr(arr4);
}
//给数组赋值
public static void assign(int[][] arr){
int count = 0;
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
arr[i][j] = count++;
}
}
}
//打印数组
public static void printArr(int[][] arr){
System.out.println("arr有" + arr.length +"行");
for(int i = 0; i < arr.length; i++){
System.out.println("第" + (i+1) + "行");
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
public class TwoDimenArrayTraverse{
public static void main(String[] args){
int arr[][] = {{1,4,3},{213,3245,5,3,4},{-1},{-12,123}};
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.println();
}
}
}
import java.util.Scanner;
public class YanghuiTriangle{
public static void main(String[] args){
while(true){
System.out.print("输入想要画出的杨辉三角的层数(-1表示结束):");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if(n == -1){
break;
}
int[][] arr = new int[n+1][n+1];
arr[1][1] = 1;
System.out.println(arr[1][1]);
for(int i = 2; i <= n; i++){
for(int j = 1; j <= i; j++){
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
}