是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据统一管理
package cn.sxu.wmy;
public class ArrayTest {
public static void main(String[] args) {
// 一维数组的声明和初始化
int ids;//声明
//静态初始化:初始化和赋值操作同时进行
ids = new int[]{1001,1002,1003,1004};
//动态初始化:初始化和赋值操作分开进行
String[] names = new String[5];
}
}
不同类型一维数组元素的默认初始化值
数组一旦初始化完成,其长度就确定了,不可修改
数组长度一旦确定不可修改
数组一旦初始化其长度是确定的
System.out.println(names.length);
System.out.println(ids.length);
for (int i = 0;i < names.length;i++)
{
System.out.println(names[i]);
}
public class ArrayTest {
public static void main(String[] args) {
// 一维数组的声明和初始化
int[] ids;//声明
//静态初始化:初始化和赋值操作同时进行
ids = new int[]{1001,1002,1003,1004};
//动态初始化:初始化和赋值操作分开进行
String[] names = new String[5];
}
}
public class ArrayTest2 {
public static void main(String[] args) {
//二维数组
//静态初始化
int[][] arr1 = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
//动态初始化
int[][] arr = new int[2][4];
int[][] arr2 = new int[3][];//行可省列不可
}
}
int[] arr = {1,2,3,4,5};
System.out.println("arr[2][1]");
for (int i = 0;i < arr1.length;i++)
{
for (int j = 0;j < arr1[i].length;j++)
{
System.out.print(arr1[i][j] + " ");
}
System.out.println();
}
public class ArrayTest1 {
public static void main(String[] args) {
int[][] arr = new int[4][3];
System.out.println(arr[0][0]);//0
System.out.println(arr[0]);//[I@6a5fc7f7
System.out.println(arr);//[[I@3b6eb2ec
System.out.println();
float[][] arr1 = new float[4][3];
System.out.println(arr1[0][0]);//0.0
System.out.println(arr1[0]);//[F@7f63425a
System.out.println();
String[][] arr2 = new String[4][3];
System.out.println(arr2[0][0]);//null
System.out.println(arr2[0]);//[Ljava.lang.String;@36d64342
System.out.println();
double[][] arr3 = new double[4][];
//System.out.println(arr[0][0]);
System.out.println(arr3[0]);//null
}
}
package cn.sxu.wmy02;
public class ArrayExer02 {
public static void main(String[] args) {
int[] arr = new int[10];
for(int i = 0; i < 10; i++)
{
arr[i] = (int)(Math.random() * 90 + 10);
System.out.print(arr[i] + " ");
}
//数组最大值
int max = arr[0];
for(int i = 0; i < arr.length; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
}
System.out.println("\n最大值为:" + max);
//数组最小值
int min = arr[0];
for(int i = 0; i < arr.length; i++)
{
if(arr[i] < min)
{
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 ava = 0;
ava = sum / arr.length;
System.out.println("平均数为:" + ava);
}
}
package cn.sxu.wmy02;
public class ArrayTest03 {
public static void main(String[] args){
String[] arr = new String[] {"AA","BB","CC", "DD", "EE", "FF"};
//数组的复制
String[] arr1 = new String[arr.length];
for(int i = 0; i < arr.length; i++)
{
arr1[i] = arr[i];
//System.out.println(arr1[i]);
}
//数组的反转
//方法一:
// for(int i = 0; i < arr.length / 2; i++)
// {
// String temp = arr[i];
// arr[i] = arr[arr.length - i -1];
// arr[arr.length - i - 1] = temp;
// }
//方法二:
for(int i = 0, j = arr.length -1; i < j; i++,j--)
{
String temp = arr[i];
arr[i] = arr[arr.length - i -1];
arr[arr.length - i - 1] = temp;
}
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
//数组的查找(线性查找)
String dest = "BB";
boolean isFlag = false;
for(int i = 0; i < arr.length; i++)
{
if(dest.equals(arr[i]))
{
System.out.println("\n找到了指定的元素,位置在:" + i);
isFlag = true;
break;
}
}
if(isFlag == false) {
System.out.println("抱歉没有找到!");
}
//数组的查找(二分法查找)
int[] arr2 = new int[] {-11, -1, 0, 12, 15, 66, 78, 980};
int dest1 = 12;
int head = 0;
int end = arr2.length - 1;
boolean isFlag1 = false;
while(head < end) {
int middle = (head + end) / 2;
if(dest1 == arr2[middle]) {
System.out.println("找到了!元素的位置在:" + middle);
isFlag1 = true;
break;
}else if(dest1 < arr2[middle]) {
end = middle - 1;
}else {
head = middle + 1;
}
}
if(isFlag1 == false) {
System.out.println("很抱歉没有找到!");
}
}
}
时间复杂度
空间复杂度
稳定性
package cn.sxu.wmy02;
public class YangHuiTest {
public static void main(String[] args) {
//声明并初始化二维数组
int[][] yangHui = new int[10][];
//二维数组赋值
for(int i = 0; i < yangHui.length; i++)
{
yangHui[i] = new int[i + 1];
yangHui[i][0] = 1;
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];
}
}
}
//遍历二维数组
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();
}
}
}
boolean equals(int[] a, int[] b)
package cn.sxu.wmy02;
import java.util.Arrays;
public class ArrayTest04 {
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)
package cn.sxu.wmy02;
import java.util.Arrays;
public class ArrayTest04 {
public static void main(String[] args) {
int[] arr1 = new int[] {1,2,3,4};
//String toString(int[] a)//输出数组信息
System.out.println(Arrays.toString(arr1));
}
}
//void fill(int[] a, int val)将指定值填充到数组中
Arrays.fill(arr1, 10);
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[] {43, 32, 76, -98, 0, 64, 33, -21, 32, 99};
int index = Arrays.binarySearch(arr3, 32);
System.out.println(index);
int[] arr1 = new int[] {1,2,3,4,5};
for(int i = 0; i <= arr1.length; i++) {
System.out.println(arr1[i]);
//第一种
int[] arr2 = new int[] {1,2,3};
arr2 = null;
System.out.println(arr2[0]);
//第二种
int[][] arr2 = new int[4][];
System.out.println(arr2[4][0]);
//第三种
String[] arr3 = new String[] {"AA", "BB", "CC"};
arr3[0] = null;
System.out.println(arr3[0].toString());
原文:https://www.cnblogs.com/wmyyy/p/ArraysJavaBlog.html