List与数组之间的相互转换、队列存取数据、栈存取数据

List与数组之间的相互转换

@Test
public void test2() {
	int[] array = { 12, 12, 1, 2 };
	System.out.println(Arrays.toString(array));
	// 数组转换为list
	List<Integer> list = Arrays.asList(12, 12, 1, 2, 2);
	System.out.println(list);
	// list转成数组
	Integer[] array2 = new Integer[list.size()];
	array2 = list.toArray(array2);
	System.out.println(Arrays.deepToString(array2));
}
@Test
	// stack:栈先进后出
	public void testStack() {
		// 栈FILO
		Stack<Integer> stack = new Stack<Integer>();
		// 存入数据
		stack.push(5);
		stack.push(6);
		stack.push(7);
		stack.push(8);
		System.out.println(stack);
		int size = stack.size();
		// 取出数据
		for (int i = 0; i < size; i++) {
			Integer t = stack.pop();
			System.out.println("取出的数据" + t);
			System.out.println("取完后剩余的数据" + stack);
		}
	}
@Test
	// queue:队列先进先出
	public void testQueue() {
		// 队列[kjuː]
		Queue<Integer> queue = new ArrayDeque<Integer>();
		queue.add(1);
		queue.add(2);
		queue.add(3);
		queue.add(4);
		int size = queue.size();
		for (int i = 0; i < size; i++) {
			Integer t = queue.poll();
			System.out.println("被移除的" + t);
			System.out.println("剩下的" + queue);
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值