Java数组

Java数组的定义和使用

Java 中定义数组的语法有两种:

  •     type arrayName[];
  •     type[] arrayName;

type 为Java中的任意数据类型,包括基本类型和组合类型,arrayName为数组名,必须是一个合法的标识符,[ ] 指明该变量是一个数组类型变量。例如:

  • int demoArray[];
  • int[] demoArray;

这两种形式没有区别,使用效果完全一样,读者可根据自己的编程习惯选择。

与C、C++不同,Java在定义数组时并不为数组元素分配内存,因此[ ]中无需指定数组元素的个数,即数组长度。而且对于如上定义的一个数组是不能访问它的任何元素的,我们必须要为它分配内存空间,这时要用到运算符new,其格式如下:

    arrayName=new type[arraySize];

其中,arraySize 为数组的长度,type 为数组的类型。如:

demoArray=new int[3];
为一个整型数组分配3个int 型整数所占据的内存空间。

通常,你可以在定义的同时分配空间,语法为:
    type arrayName[] = new type[arraySize];
例如:

int demoArray[] = new int[3];

数组的初始化

你可以在声明数组的同时进行初始化(静态初始化),也可以在声明以后进行初始化(动态初始化)。例如:

// 静态初始化
// 静态初始化的同时就为数组元素分配空间并赋值
int intArray[] = {1,2,3,4};
String stringArray[] = {"微学苑", "http://www.weixueyuan.net", "一切编程语言都是纸老虎"};
// 动态初始化
float floatArray[] = new float[3];
floatArray[0] = 1.0f;
floatArray[1] = 132.63f;
floatArray[2] = 100F;

数组引用

可以通过下标来引用数组:
    arrayName[index];
与C、C++不同,Java对数组元素要进行越界检查以保证安全性。

每个数组都有一个length属性来指明它的长度,例如 intArray.length 指明数组 intArray 的长度。

【示例】写一段代码,要求输入任意5个整数,输出它们的和。

import java.util.*;
public class Demo {
public static void main(String[] args){
int intArray[] = new int[5];
long total = 0;
int len = intArray.length;
// 给数组元素赋值
System.out.print("请输入" + len + "个整数,以空格为分隔:");
Scanner sc = new Scanner(System.in);
for(int i=0; i<len; i++){
intArray[i] = sc.nextInt();
}
// 计算数组元素的和
for(int i=0; i<len; i++){
total += intArray[i];
}
System.out.println("所有数组元素的和为:" + total);
}
}

运行结果:
请输入5个整数,以空格为分隔:10 20 15 25 50
所有数组元素的和为:120

数组的遍历

实际开发中,经常需要遍历数组以获取数组中的每一个元素。最容易想到的方法是for循环,例如:

实际开发中,经常需要遍历数组以获取数组中的每一个元素。最容易想到的方法是for循环,例如:

int arrayDemo[] = {1, 2, 4, 7, 9, 192, 100};
for(int i=0,len=arrayDemo.length; i<len; i++){
System.out.println(arrayDemo[i] + ", ");
}

输出结果:
1, 2, 4, 7, 9, 192, 100,

不过,Java提供了”增强版“的for循环,专门用来遍历数组,语法为:

for( arrayType varName: arrayName ){
// Some Code
}

arrayType 为数组类型(也是数组元素的类型);varName 是用来保存当前元素的变量,每次循环它的值都会改变;arrayName 为数组名称。

每循环一次,就会获取数组中下一个元素的值,保存到 varName 变量,直到数组结束。即,第一次循环 varName 的值为第0个元素,第二次循环为第1个元素......例如:

int arrayDemo[] = {1, 2, 4, 7, 9, 192, 100};
for(int x: arrayDemo){
System.out.println(x + ", ");
}

输出结果与上面相同。

这种增强版的for循环也被称为”foreach循环“,它是普通for循环语句的特殊简化版。所有的foreach循环都可以被改写成for循环。

但是,如果你希望使用数组的索引,那么增强版的 for 循环无法做到。

二维数组

二维数组的声明、初始化和引用与一维数组相似:

int intArray[ ][ ] = { {1,2}, {2,3}, {4,5} };
int a[ ][ ] = new int[2][3];
a[0][0] = 12;
a[0][1] = 34;
// ......
a[1][2] = 93;

Java语言中,由于把二维数组看作是数组的数组,数组空间不是连续分配的,所以不要求二维数组每一维的大小相同。例如:

int intArray[ ][ ] = { {1,2}, {2,3}, {3,4,5} };
int a[ ][ ] = new int[2][ ];
a[0] = new int[3];
a[1] = new int[5];

【示例】通过二维数组计算两个矩阵的乘积。

public class Demo {
public static void main(String[] args){
// 第一个矩阵(动态初始化一个二维数组)
int a[][] = new int[2][3];
// 第二个矩阵(静态初始化一个二维数组)
int b[][] = { {1,5,2,8}, {5,9,10,-3}, {2,7,-5,-18} };
// 结果矩阵
int c[][] = new int[2][4];
// 初始化第一个矩阵
for(int i=0; i<2; i++)
for(int j=0; j<3 ;j++)
a[i][j] = (i+1) * (j+2);
// 计算矩阵乘积
for (int i=0; i<2; i++){
for (int j=0; j<4; j++){
c[i][j]=0;
for(int k=0; k<3; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
// 输出结算结果
for(int i=0; i<2; i++){
for (int j=0; j<4; j++)
System.out.printf("%-5d", c[i][j]);
System.out.printlnwww.ymzxrj.com();
}
}
}

运行结果:
25   65   14   -65 
50   130  28   -130

几点说明:

上面讲的是静态数组。静态数组一旦被声明,它的容量就固定了,不容改变。所以在声明数组时,一定要考虑数组的最大容量,防止容量不够的现象。
如果想在运行程序时改变容量,就需要用到数组列表(ArrayList,也称动态数组)或向量(Vector)。
正是由于静态数组容量固定的缺点,实际开发中使用频率不高,被 ArrayList 或 Vector 代替,因为实际开发中经常需要向数组中添加或删除元素,而它的容量不好预估。

数组的方法(查找,插入,删除,打印)

/**
 * 普通数组的Java代码
 * @author dream
 *
 */
public class GeneralArray {

	private int[] a;
	private int size;   //数组的大小
	private int nElem;   //数组中有多少项
	
	public GeneralArray(int max){
		this.a = new int[max];
		this.size = max;
		this.nElem = 0;
	}
	
	public boolean find(int searchNum){   //查找某个值
		int j;
		for(j = 0; j < nElem; j++){
			if(a[j] == searchNum){
				break;
			}
		}
		if(j == nElem){
			return false;
		}else {
			return true;
		}
	}
	
	
	public boolean insert(int value){   //插入某个值
		if(nElem == size){
			System.out.println("数组已满");
			return false;
		}
		a[nElem] = value;
		nElem++;
		return true;
	}
	
	public boolean delete(int value){   //删除某个值
		int j;
		for(j = 0; j < nElem; j++){
			if(a[j] == value){
				break;
			}
		}
		if(j == nElem){
			return false;
		}
		if(nElem == size){
			for(int k = j; k < nElem - 1; k++){
				a[k] = a[k+1];
			}
		}else {
			for(int k = j; k < nElem; k++){
				a[k] = a[k+1];
			}
		}
		nElem--;
		return true;
	}
	
	public void display(){   //打印整个数组
		for(int i = 0; i < nElem; i++){
			System.out.println(a[i] + " ");
		}
		System.out.println("");
	}
}

数组的数据结构

  • 线性查找的话,时间复杂度为O(N),
  •  二分查找的话时间为O(longN),
  • 无序数组插入的时间复杂度为O(1),
  • 有序数组插入的时间复杂度为O(N),
  • 删除操作的时间复杂度均为O(N)。
/**
 * 有序数组的Java代码
 * @author dream
 *
 */

/**
 * 对于数组这种数据结构,
 * 线性查找的话,时间复杂度为O(N),
 * 二分查找的话时间为O(longN),
 * 无序数组插入的时间复杂度为O(1),
 * 有序数组插入的时间复杂度为O(N),
 * 删除操作的时间复杂度均为O(N)。
 * @author dream
 *
 */
public class OrderedArray {

	private long[] a;
	private int size;   //数组的大小
	private int nElem;  //数组中有多少项
	
	public OrderedArray(int max){   //初始化数组
		this.a = new long[max];
		this.size = max;
		this.nElem = 0;
	}
	
	public int size(){   //返回数组实际有多少值
		return this.nElem;
	}
	
	/**
	 * 二分查找
	 * @param searchNum
	 * @return
	 */
	public int find(long searchNum){
		int lower = 0;
		int upper = nElem - 1;
		int curr;
		while (true) {
			curr = (lower + upper) / 2;
			if(a[curr] == searchNum){
				return curr;
			}else if(lower > upper){
				return -1;
			}else {
				if(a[curr] < searchNum){
					lower = curr + 1;
				}else {
					upper = curr - 1;
				}
			}
		}
	}
	
	
	public boolean insert(long value){   //插入某个值
		if(nElem == size){
			System.out.println("数组已满!");
			return false;
		}
		int j;
		for(j = 0; j < nElem; j++){
			if(a[j] > value){
				break;
			}
		}
		
		for(int k = nElem; k > j; k++){
			a[k] = a[k-1];
		}
		a[j] = value;
		nElem++;
		return true;
	}
	
	
	
	public boolean delete(long value){   //删除某个值
		int j = find(value);
		if(j == -1){
			System.out.println("没有该元素!");
			return false;
		}
		
		if(nElem == size){
			for(int k = j; k < nElem - 1; k++){
				a[k] = a[k+1];
			}
			a[nElem-1] = 0;
		}else {
			for(int k = j; k < nElem; k++){
				a[k] = a[k+1];
			}
		}
		nElem--;
		return true;
	}
	
	
	public void display(){   //打印整个数组
		for(int i = 0; i < nElem; i++){
			System.out.println(a[i] + " ");
		}
		System.out.println("");
	}
}

原文来源

https://www.cnblogs.com/ok932343846/p/6743699.html

https://github.com/francistao/LearningNotes/blob/master/Part3/DataStructure/%E6%95%B0%E7%BB%84.md

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值