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);
		}
	}

版权声明:本文为weixin_43532218原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。