Learn from teacher Wang (iv): Traversing elements with a foreach loop

Source: Internet
Author: User

Traversing elements with a foreach loop
Speaker: Wang Shaohua QQ Group No.: 483773664
Learning Goals:

1. Master using the Foreach loop to iterate through the elements


JDK1.5 and later versions, you can iterate through the collection elements through foreach.

First, use foreach to traverse the Dog collection
12345678910111213141516171819202122 public class Test {    public static void main(String[] args) {        // 1、创建四个狗狗对象        Dog ououDog = new Dog("欧欧", "雪娜瑞");        Dog yayaDog = new Dog("亚亚", "拉布拉多");        Dog meimeiDog = new Dog("美美", "雪娜瑞");        Dog feifeiDog = new Dog("菲菲", "拉布拉多");        // 2、创建ArrayList集合对象并把四个狗狗对象放入其中        List dogs = new ArrayList();        dogs.add(ououDog);        dogs.add(yayaDog);        dogs.add(meimeiDog);        dogs.add(2, feifeiDog); // 添加feifeiDog到指定位置        // 3、输出集合中狗狗的数量        System.out.println("共计有" + dogs.size() + "条狗狗。");        // 4、通过foreach遍历集合显示各条狗狗信息        for(Object object :dogs){            Dog dog = (Dog) object;            System.out.println(dog.getName() + "\t" + dog.getStrain());        }    }}Learn from teacher Wang (iv): Traversing elements with a foreach loop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.