ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

集合训练1

2022-01-19 21:34:56  阅读:239  来源: 互联网

标签:return String 训练 title content 集合 News public


集合训练

编程题

按要求实现:

  1. 封装一个新闻类,包含标题和内容属性,提供get、set方法,重写toString方法,打印对象时只打印标题;
  2. 只提供一个带参数的构造器,实例化对象时,只初始化标题;并且实例化两个对象:新闻一:新冠确诊病例超千万,数百万印度教信徒赴恒河“圣浴”引民众担忧
    新闻二:男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生
  3. 将新闻对象添加到ArrayList集合中,并且进行倒序遍历;
  4. 在遍历集合过程中,对新闻标题进行处理,超过15字的只保留前15个,然后在后边加“….”(5)在控制台打印遍历出经过处理的新闻标题;
import java.util.ArrayList;

@SuppressWarnings({"all"})
public class Homework01 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new News("新冠确诊病例超千万,数百万印度教信徒赴恒河\"圣浴\"引民众担忧", ""));
        arrayList.add(new News("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生",""));

        int size = arrayList.size();
        for (int i = size - 1; i >= 0 ; i--) {
            News news = (News) arrayList.get(i);
            System.out.println(processTitle(news.getTitle()));
        }
    }

    //处理新闻标题
    public static String processTitle(String title){
        if (title == null){
            return "";
        }

        if (title.length()>15){
            return title.substring(0, 15) + "...";//(0,15]
        }
        else{
            return title;
        }
    }
}

class News{
    private String title;
    private String content;

    public News(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "news{" +
                "title='" + title +
                '}';
    }
}

标签:return,String,训练,title,content,集合,News,public
来源: https://www.cnblogs.com/wshjyyysys/p/15824184.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有