首页 > 编程语言 > 详细

集合转数组遍历。

时间:2020-12-29 00:02:42      阅读:21      评论:0      收藏:0      [点我收藏+]

package com.heima.collection;

import java.util.ArrayList;
import java.util.Collection;

import com.heima.bean.Student;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo03 {
public static void main(String[] args) {
test01();
Collection c = new ArrayList();
c.add(new Student("张三", 23));// Object obj = new Student("张三",23);
c.add(new Student("李四", 21));
c.add(new Student("王五", 22));
Object[] arr = c.toArray();// 将集合转换成数组
for (int i = 0; i < arr.length; i++) {
// 写法一:
System.out.println(arr[i]);
// 写法二:
Student s = (Student) arr[i];// 将object类型转换为student类型
System.out.println(s.getAge());
/*
* 这两种写法区别:写法一是直接打印,写法二可以打印也可以获取到属性值,其他地方也可以利用(存储)。
*/
}
}

private static void test01() {
Collection c = new ArrayList();
c.add("a");
c.add("a");
c.add("a");
c.add("a");
Object[] arr = c.toArray(); // 将集合转换成数组
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}

集合转数组遍历。

原文:https://www.cnblogs.com/wangffeng293/p/14203914.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!