关注
import java.util.ArrayList; import java.util.Iterator; import java.util.List; interface Notepad { // 能存储记录 // 不限制能存储的记录条数 public void addNote(String note); // 能知道已经存储的记录的数量 public int getNoteLength(); // 能查看每一条记录 public String getOne(int position); // 能删除一条记录 public String deleteOne(int position); // 能按笔记内容删除一条笔记 public String deleteOne(String note); // 能列出所有的记录 public String getAllNotes(); } public class NotepadbyList implements Notepad { private List<String> notes; // 能存储记录 // 不限制能存储的记录条数 public void addNote(String note) { notes.add(note); } // 能知道已经存储的记录的数量 public int getNoteLength() { return notes.size(); } // 能查看每一条记录 public String getOne(int position) { return notes.get(position); } // 能删除一条记录 public String deleteOne(int position) { return notes.remove(position); } // 能按笔记内容删除一条笔记 public String deleteOne(String note) { Iterator<String> iterator = notes.iterator(); while (iterator.hasNext()) { String str = iterator.next(); if (str.equals(note)) { iterator.remove(); return note; } } return ""; } // 能列出所有的记录 public String getAllNotes() { StringBuilder sb = new StringBuilder(); for (String string : notes) { sb.append(string); } return sb.toString(); } // 构造函数 public NotepadbyList() { notes = new ArrayList<String>(); } // 测试方法 public static void main(String args[]) { NotepadbyList np = new NotepadbyList(); np.addNote("好好学习");// 添加笔记 np.addNote("天天向上");// 添加笔记 np.addNote("少玩游戏");// 添加笔记 np.addNote("多刷牛客");// 添加笔记 System.out.println(np.getNoteLength());// 这儿应该输出4 System.out.println(np.getOne(3));// 这儿应该输出多刷牛客 System.out.println(np.getAllNotes());// 这儿应该输出好好学习天天向上少玩游戏多刷牛客 System.out.println(np.deleteOne(0));// 这儿应该输出好好学习 System.out.println(np.deleteOne("天天向上"));// 这儿应该输出天天向上 np.addNote("好好学习");// 添加笔记 np.addNote("天天向上");// 添加笔记 System.out.println(np.getAllNotes());// 这儿应该输出少玩游戏多刷牛客好好学习天天向上 } }
查看原帖
点赞 3
相关推荐
10-17 15:37
浙江大学 深度学习 牛客551960008号:是投的太晚了么,九月开始都陆续oc了,后面面的应该比较难?在等大佬们鸽offer了
投递美团等公司10个岗位 > 你都收到了哪些公司的感谢信?
点赞 评论 收藏
分享
很有精神123:吴思远加油
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
正在热议
# 同bg的你秋招战况如何? #
17636次浏览 142人参与
# 写简历别走弯路 #
579974次浏览 7192人参与
# 0offer互助地 #
120430次浏览 1230人参与
# 你的简历改到第几版了 #
641592次浏览 9443人参与
# 我的简历长这样 #
1631943次浏览 25631人参与
# 寒假躺平还是提前实习 #
21555次浏览 91人参与
# 你会为了工作牺牲生活吗? #
18038次浏览 159人参与
# 国企vs私企,你更想去? #
77184次浏览 942人参与
# 如何写一份好简历 #
553559次浏览 8039人参与
# 第一份工作应该只看薪资吗 #
44967次浏览 812人参与
# 最后再改一次简历 #
1708709次浏览 27266人参与
# 正在实习的碎碎念 #
1172977次浏览 12438人参与
# 实习与准备秋招该如何平衡 #
600234次浏览 7432人参与
# 工作两年想退休了 #
43397次浏览 551人参与
# 你们的毕业论文什么进度了 #
762116次浏览 7655人参与
# 非技术岗薪资爆料 #
135259次浏览 1239人参与
# 蚂蚁求职进展汇总 #
13398次浏览 185人参与
# 工作压力大怎么缓解 #
29649次浏览 442人参与
# offer帮选 #
210777次浏览 2156人参与
# 能让你振作起来的一句话 #
32817次浏览 329人参与