python for倒序遍历数组_59.for循环遍历数组、集合(含代码)

undefined_1440w.jpg?source=172ae18b

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) {
		ArrayList<People> people = 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();
		Collection<String> collection = 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
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值