首页 > 其他分享 >遍历集合的案例

遍历集合的案例

时间:2023-06-01 19:34:17浏览次数:39  
标签:遍历 String Movie actor 案例 score 集合 public name

 

package com.Movie1;

public class Movie {
    private String name;
    private double score;
    private String actor;

    public Movie() {
    }

    public Movie(String name, double score, String actor) {
        this.name = name;
        this.score = score;
        this.actor = actor;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }

    @Override
    public String toString() {
        return "Movie{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", actor='" + actor + '\'' +
                '}';
    }
}
package com.Movie1;
/*
*目标:完成电影信息下展示
* new Movie("《肖申克的救赎》",9.7,"罗宾斯")
* new Movie("《霸王别姬》",9.6,"张国荣、张丰毅")
* new Movie("《阿甘正传》",9.5,"汤姆.汉克斯")
*
*
* */

import java.util.ArrayList;
import java.util.Collection;

public class CollectionTest {
    public static void main(String[] args) {
        //1.创建一个集合容器负责储存多部电影对象
        Collection<Movie> movies=new ArrayList<>();
        movies.add(new Movie("《肖申克的救赎》",9.7,"罗宾斯"));
        movies.add(new Movie("《霸王别姬》",9.6,"张国荣、张丰毅"));
        movies.add(new Movie("《阿甘正传》",9.5,"汤姆.汉克斯"));
        System.out.println(movies);

        for (Movie movie : movies) {
            System.out.println("电影名:"+movie.getName());
            System.out.println("评分:"+movie.getScore());
            System.out.println("主演:"+movie.getActor());
            System.out.println("-------------------------------------------------");
        }

    }
}

 

 

标签:遍历,String,Movie,actor,案例,score,集合,public,name
From: https://www.cnblogs.com/Karl-hut/p/17449996.html

相关文章