Java编程思想笔记16——第16章:数组

16 数组

16.1 数组为什么特殊

​ Java中有大量的持有对象(Collection和Map),为什么还要用数组?

​ 数组存储效率最高,随机访问速度快。但是数组的大小是固定的,并且不能更改。

​ 如果下标超过了数组的长度,就会出现数组越界(RuntimeException)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test4 {
	public static void main(String[] args) {	
		List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6));
		System.out.println(list);	
		
		int[] a = new int[] {1,2,3,4,5,6};
		System.out.println(Arrays.toString(a));
	}

}

16.2 数组是第一级对象

​ 数组存储的是一个引用,指向真正的对象。

初始化数组的方法如下

class Horse{}
public class Test4 {
	public static void main(String[] args) {	
		Horse[] a ; //仅仅只是一个数组指针
		Horse[] b = new Horse[5]; //指向一个长度为5的数组
		Horse[] d = {new Horse(), new Horse(),new Horse()};//直接赋初值
		
	}

}

16.3 返回一个数组

​ 如果想要在函数中返回多个值,可以试着返回一个数组。

import java.util.Arrays;

public class Test4 {
	
	public static String[] fun() {	
		String[] str_list = {"zhangsan","lisi"};
		return str_list;
	}
	
	public static void main(String[] args) {	
		String[] res = fun();
		System.out.println(Arrays.toString(res));
	}

}

16.4 多维数组

​ 多维数组,那么需要多个括号进行分割。

public class Test4 {
	public static void main(String[] args) {	
		int[][] array = {
				{1,2,3},{4,5,6}
		};
		// deepToString 专门用来打印二维数组
		System.out.println(Arrays.deepToString(array));	
		
        
        // 三维数组
        int[][][] array2 = new int[2][3][4];
		System.out.println(Arrays.deepToString(array2));	
        
	}

}

每个维度的长度可以不固定

import java.util.Arrays;
import java.util.Random;

public class Test4 {
	public static void main(String[] args) {	
		Random rand = new Random();
		
		int[][] array = new int[2][];//第1个维度为2
		
		for (int i = 0; i < array.length; i++) {
			array[i] = new int[rand.nextInt(5)];// 第二个维度则是随机数
		}
		System.out.println(Arrays.deepToString(array));
		
	}
}
import java.util.Arrays;

public class Test4 {
	public static void main(String[] args) {	
		
		int[][] array = {
				{1,2,3},
				{4,5},
				{6,7,8,9,10}
		};
		System.out.println(Arrays.deepToString(array));
		
	}
}
// [[1, 2, 3], [4, 5], [6, 7, 8, 9, 10]]

16.5 数组与泛型

​ 数组不能直接使用泛型,但是容器(List)可以。

​ 比如List<Integer>,这在数组中是不可以的。

​ 不过数组可以在类中使用泛型。

import java.util.Arrays;
class Parameter<T>{
	public <T> T[] fun(T[] list) {
		return list;	
	}
}


public class Test4 {
	public static void main(String[] args) {	
		Integer[] arr = {4,3,2,4,6};
		Parameter<Integer> p = new Parameter<Integer>();
		
		Integer[] list = p.fun(arr);
		System.out.println(Arrays.toString(list));
		
		
	}
}

16.6 创建测试数据

​ 测试的时候可以很方便的填充数据

16.6.1 Arrays.fill()

​ 填充一样的数据

public class Test4 {
	public static void main(String[] args) {	
		
		String[] name_list = new String[6];
		Arrays.fill(name_list, "lisi");
		System.out.println(Arrays.toString(name_list));	
	}
}
// [lisi, lisi, lisi, lisi, lisi, lisi]

16.6.2 数据生成器

interface Generator<T>{//生成器的接口
	T next();
}


class GenClass{//生成器类
	public static class IntGen implements Generator<Integer>{//内部类
		private int count=0;
		@Override
		public Integer next() {
			return count++;
		}		
	}
	
	public static class LongGen implements Generator<Long>{//内部类
		private long count=0;
		@Override
		public Long next() {
			return count++;
		}		
	}
	
}

public class Test4 {
	public static void main(String[] args) {	
		
		GenClass.IntGen intGen = new GenClass.IntGen();
		for (int i = 0; i < 10; i++) {
			System.out.println(intGen.next());
		}
		
	}
}

16.7 Arrays实用功能

​ Arrays中有六个基本方法,

  1. equals() 比较两个数组是否相等
  2. fill() 所有数据填充一样的
  3. sort() 排序
  4. binarySearch() 二分查找
  5. toString()
  6. hashCode() 产生散列码
  7. asList() 接受可变参数

16.7.1 复制数组

public class Test4 {
	public static void main(String[] args) {	
		
		int[] a = new int[10];
		int[] b = new int[12];
		Arrays.fill(a, 7);
		Arrays.fill(b, 22);
		// 数组复制, a数组从0开始, b数组从0开始。 a复制到b。长度为a.length
		System.arraycopy(a, 0, b, 0, a.length);
		System.out.println(Arrays.toString(b));
	}
}

16.7.2 数组的比较

public class Test4 {
	public static void main(String[] args) {	
		
		int[] a = new int[10];
		int[] b = new int[12];
		Arrays.fill(a, 7);
		Arrays.fill(b, 22);
		System.out.println(Arrays.equals(a, b));//false
		
		
		String[] c = {"zs","lisi"};
		String[] d = {"zs","lisi"};
		System.out.println(Arrays.deepEquals(c, d));//true
	}
}
// false
// true

16.7.3 数组元素的比较

class Score implements Comparable<Score>{//实现了比较的接口
	private int math;
	private int chinese;
	private int sum;
	
	public Score(int math, int chinese) {
		this.math = math;
		this.chinese = chinese;
		this.sum = math+chinese;
	}

	@Override
	public int compareTo(Score o) {
		// 大于返回大于0, 小于返回小于0,相等为0
		return this.sum - o.sum ;
	}
	
	@Override
	public String toString() {
		return ""+this.sum;
	}
}

public class Test4 {
	public static void main(String[] args) {	
		Score[] list = {new Score(44,55),new Score(33,22),new Score(99,100)};
		
		Arrays.sort(list);
		System.out.println(Arrays.toString(list));
	}
}

16.7.4 数组排序

public class Test4 {
	public static void main(String[] args) {	
		int[] list = {55,33,22,88,100};
		Arrays.sort(list);
		
		String[] str_list = {"zs","lisi"};
		Arrays.sort(str_list);
		
		
	}
}

16.7.5 二分查找

public class Test4 {
	public static void main(String[] args) {	
		int[] list = {55,33,22,88,100};
		Arrays.sort(list);
		System.out.println(Arrays.toString(list));
		
		int res = Arrays.binarySearch(list, 33);
		System.out.println(res);
		
	}
}

16.8 总结

​ 对于数组,主要主要是性能比容器高。但是灵活度没有容器高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值