集合list里存储list,要遍历最底层list的对象元素的遍历方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合list里存储list,要遍历最底层list的对象元素的遍历方法相关的知识,希望对你有一定的参考价值。
1 package com.wtd; 2 3 import java.util.ArrayList; 4 import java.util.HashSet; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Set; 8 9 public class L16 { 10 11 /** 12 * @desc 需要将list传到另个方法里怎么用list遍历userList和dpList? 13 * @param args 14 * @throws Exception 15 */ 16 public static void main(String[] args) throws Exception { 17 // TODO Auto-generated method stub 18 List list = new ArrayList<ArrayList>(); 19 User user= new User(); 20 user.setAge(18); 21 user.setName("andi"); 22 Department department= new Department(); 23 department.setDepartId(7); 24 department.setDepartName("Program"); 25 List<User> userList = new ArrayList();//用于接收从数据库查出的数据 26 userList.add(user); 27 List<Department> dpList = new ArrayList();//用于接收从数据库查出的数据 28 dpList.add(department); 29 list.add(userList); 30 list.add(dpList); 31 getElement(list); 32 } 33 public static void getElement(List list) throws Exception{ 34 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 35 ArrayList listSmall = (ArrayList) iterator.next(); 36 for (Iterator iterator2 = listSmall.iterator(); iterator2.hasNext();) { 37 Object objectSmall= iterator2.next(); 38 String className = objectSmall.getClass().getName(); 39 if ("com.wtd.User".equalsIgnoreCase(className)) {//com.wtd.User 40 System.out.println("className.user="+className); 41 User userSmall= (User) objectSmall; 42 System.out.println("user.name:"+userSmall.getName()+"\\r\\nuser.age:"+userSmall.getAge()); 43 }else if ("com.wtd.Department".equalsIgnoreCase(className)) { 44 System.out.println("className.department="+className);//com.wtd.Department 45 Department departmentSmall= (Department) objectSmall; 46 System.out.println("department.departname:"+departmentSmall.getDepartName()+"\\r\\ndepartment.departid:"+departmentSmall.getDepartId()); 47 } 48 49 } 50 51 } 52 53 } 54 55 } 56 57 class User{ 58 int age; 59 String name; 60 public int getAge() { 61 return age; 62 } 63 public void setAge(int age) { 64 this.age = age; 65 } 66 public String getName() { 67 return name; 68 } 69 public void setName(String name) { 70 this.name = name; 71 } 72 73 } 74 75 class Department{ 76 int departId; 77 String departName; 78 public int getDepartId() { 79 return departId; 80 } 81 public void setDepartId(int departId) { 82 this.departId = departId; 83 } 84 public String getDepartName() { 85 return departName; 86 } 87 public void setDepartName(String departName) { 88 this.departName = departName; 89 } 90 91 }
解决此URL提出的问题:http://q.cnblogs.com/q/79494/
以上是关于集合list里存储list,要遍历最底层list的对象元素的遍历方法的主要内容,如果未能解决你的问题,请参考以下文章