1. 程式人生 > >list集合轉對應型別的物件陣列

list集合轉對應型別的物件陣列

package com.mbit.test1;

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

public class TestReturnArray {
	
	public static void main(String[] args) {
		
		ArrayList<Teacher> list = (ArrayList<Teacher>) returnList();
		
		Teacher[] arrTec = new Teacher[list.size()];
		
		arrTec = list.toArray(arrTec);
		
		System.out.println(Arrays.toString(arrTec));
		System.out.println(list.getClass());
		
		
	}
	
	public static List<Teacher> returnList(){
		
		Teacher t1 = new Teacher("1","name","ava");
		
		ArrayList<Teacher> a1 = new ArrayList<>();
		a1.add(t1);
		return  a1;
	}

}




class Teacher{
	private String name;
	
	private String age;
	
	private String classroom;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getClassroom() {
		return classroom;
	}

	public void setClassroom(String classroom) {
		this.classroom = classroom;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", classroom=" + classroom + "]";
	}

	public Teacher(String name, String age, String classroom) {
		super();
		this.name = name;
		this.age = age;
		this.classroom = classroom;
	}

	public Teacher() {
		super();
	}
	
	
}