Array
定义
- 1.Java语言中的数组是一种引用数据类型,不属于基本数据类型。
- 2.数组的父类是Object。
- 3.数组实际上是一个容器,可以同时容纳多个元素。
- 4.数组可以存储基本数据类型的数据,也可以存储引用数据类型的数据。
- 5.因为数组是引用类型,所以数组对象在堆内存中。
- 6.数组当中如果存储的是“java对象”的话,实际上存储的是对象的“引用(内存地址)”,数组中不能直接存储java对象。
- 7.数组一旦创建,在java中规定,长度不可变。
- 8.所有的数组对象都有length属性(java自带的),用来获取数组中元素的个数。
- 9.java中的数组要求数组中的数据类型统一。比如int类型数组只能存储int类型,Person类型数组只能存储Person类型。
- 10.数组在内存方面,数组中的数据元素内存地址是连续的。
- 11.所有的数组都是拿“第一个小方框的内存地址”作为整个数组对象的内存地址。因为其存储空间是连续的,后续的内存地址都可以算出来的。
- 12.优点:查询/查找/检索某个下标的元素时效率极高。效率也是最高的。
缺点:由于随机增删元素会涉及移动后面的元素的操作,所以增删效率较低。其次很难找到内存空间上特别大的连续的内存空间。
注意:对于数组中的最后一个元素的增删,是没有效率影响的。 - 13.怎么声明/定义一个一维数组?
int[] array1;
double[] array2;
boolean[] array3;
String[] array4;
Object[] array5;
静态初始化: int[] array = {100,2100,300,55};
动态初始化: int[] array = new int[5];//初始化长度为5的int型数组,每个元素默认值为0
String[] names = new String[6];//初始化6个长度的String类型数组,每个元素默认值null。
查询数组
public class interfaceTest {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5, 6};//方法一、静态数组
int[] array2 = new int[5];//方法二、动态调用
System.out.println("JVM调用的main方法args数组长度为: " + args.length);
System.out.println("array1数组的长度为: " + array1.length);
System.out.println("array2数组的长度为: " + array2.length);
int len1 = array1.length;
int len2 = array2.length;
for(int i = 0;i<len1;i++){
System.out.print(array1[i]);
}
for(int i = 0;i<len2;i++){
System.out.print(array2[i]);
}
}
}
输出
JVM调用的main方法args数组长度为: 0
array1数组的长度为: 6
array2数组的长度为: 5
12345600000
将数组作为参数传递
有两种方式,一种是将定义好的数组传入,另一种是直接传递一个静态数组(要求能读懂)
public class InterfaceTest {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5, 6};//方法一、静态数组
Func func = new Func();
System.out.println(func.compute(array1));
System.out.println(func.compute(new int[]{7,8,9,10}));
}
}
/*
* 求数组和
* */
class Func{
int compute(int[] array){
int sum = 0;
for(int i = 0;i<array.length;i++){
sum += array[i];
}
return sum;
}
}
输出
21
34
一个有趣的例子----模拟系统登录
public class InterfaceTest {
public static void main(String[] args) {
if(args.length != 2){
System.out.println("使用系统时,请输入账号和密码信息,例如:my 123");
}
String username = args[0];
String password = args[1];
if("admin".equals(username)&& "123".equals(password)){
System.out.println("登录成功!欢迎" + username + "回来...");
System.out.println("欢迎使用本系统!");
}else{
System.out.println("验证失败,请输入正确的账号和密码");
}
}
}
输出
使用系统时,请输入账号和密码信息,例如:my 123
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at InterfaceTest.main(InterfaceTest.java:6)
设置输入参数为admin 123后
输出
登录成功!欢迎admin回来...
欢迎使用本系统!
数组中存储引用数据类型
public class InterfaceTest {
public static void main(String[] args) {
Cat c = new Cat();
Bird b = new Bird();
Animal[] animal = {c,b};
System.out.println(animal[0]+ " " + animal[1]);
for(int i = 0;i<animal.length;i++){
if(animal[i] instanceof Cat){
Cat cat = (Cat)animal[i];
cat.catchmouse();
}else{
Bird bird = (Bird)animal[i];
bird.sing();
}
}
}
}
class Animal{
}
class Cat extends Animal{
void catchmouse(){
System.out.println("抓老鼠!");
}
}
class Bird extends Animal{
void sing(){
System.out.println("唱歌");
}
}
输出
Cat@7cc355be Bird@6e8cf4c6
抓老鼠!
唱歌
JAVA数组扩容
java中对数组扩容:先新建一个大容量的数组,然后将小容量数组中的数据一个一个的拷贝到大数组中,所以扩容效率较低
所以在开发过程中尽可能少的进行数组的拷贝.开发中估计多一些长度.
扩容方法System.arraycopy
public class InterfaceTest {
public static void main(String[] args) {
int[] a = {1,3,5,7,9};
int[] b = new int[10];
System.arraycopy(a,2,b,3,3);
for(int i = 0;i<b.length;i++){
System.out.print(b[i]);
}
}
}
输出
0005790000
打完收工!
觉得本文不错的话留个赞再走哦~