1、泛型数组遍历出来,集合用ArrayList,遍历格式为(foreach):
for(类(将所有类型带过来) 循环变量名称(自定义一个e): 要被遍历的对象){}
2、for(元素的数据类型(int这些,包装类型integer也可) 变量自定义一个 : Collection集合or数组){
}3、上代码:People.java
package com.equals;
public class People {
private Integer id;
private String name;
private String post;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public People(String name,String post){
// this.id = id;
this.name = name;
this.post = post;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + ''' +
", post='" + post + ''' +
'}';
}
}
Test.java
package com.equals;
import java.util.ArrayList;
import java.util.Collection;
public class test{
public static void main(String[] args) {
ArrayListpeople = new ArrayList<>();
people.add(new People("zhoonghau","php"));
people.add(new People("zhoonghauzhognhuada","java1000"));
people.add(new People("zhoonghau3","php1111"));
for (People e:people
) {
System.out.println("People{" +
"id=" + e.getId() +
", name='" + e.getName() + ''' +
", post='" + e.getPost() + ''' +
'}');
}
String[] arr = new String[]{"学生","职场人","管理员"};
Integer[] arr2 = new Integer[]{1,2,23};
for (Integer w:arr2
) {
System.out.print(w+" ");
}
System.out.println();
Collectioncollection = new ArrayList<>();
collection.add("jiyu "+"java");
collection.add("xiaaohuadonghe");
collection.add("zhongtongsida");
for (String z:collection){
System.out.println(z);
}
}
}
4、结果
People{id=null, name='zhoonghau', post='php'}
People{id=null, name='zhoonghauzhognhuada', post='java1000'}
People{id=null, name='zhoonghau3', post='php1111'}
1 2 23
jiyu java
xiaaohuadonghe
zhongtongsida