- 1、本文档共41页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Out-of-bounds Legal indexes are between 0 and the lists size() - 1. Reading or writing any index outside this range will cause an IndexOutOfBoundsException. ArrayListString names = new ArrayListString(); names.add(Marty); names.add(Kevin); names.add(Vicki); names.add(Larry); System.out.println(names.get(0)); // okay System.out.println(names.get(3)); // okay System.out.println(names.get(-1)); // exception names.add(9, Aimee); // exception index 0 1 2 3 value Marty Kevin Vicki Larry ArrayList mystery ArrayListInteger list = new ArrayListInteger(); for (int i = 1; i = 10; i++) { list.add(10 * i); // [10, 20, 30, 40, ..., 100] } What is the output of the following code? for (int i = 0; i list.size(); i++) { list.remove(i); } System.out.println(list); Answer: [20, 40, 60, 80, 100] ArrayList mystery 2 ArrayListInteger list = new ArrayListInteger(); for (int i = 1; i = 5; i++) { list.add(2 * i); // [2, 4, 6, 8, 10] } What is the output of the following code? int size = list.size(); for (int i = 0; i size; i++) { list.add(i, 42); // add 42 at index i } System.out.println(list); Answer: [42, 42, 42, 42, 42, 2, 4, 6, 8, 10] ArrayList as parameter public static void name(ArrayListType name) { Example: // Removes all plural words from the given list. public static void removePlural(ArrayListString list) { for (int i = 0; i list.size(); i++) { String str = list.get(i); if (str.endsWith(s)) { list.remove(i); i--; } } } You can also return a list: public static ArrayListType methodName(params) Exercise Write a method addStars that accepts an array list of strings as a parameter and places a * after each element. Example: if an array list named list initially stores: [the, quick, brown, fox] Then the call of addStars(list); makes it store: [the, *, quick, *, brown, *, fox, *] Write a method removeStars that accepts an array list of strings,
您可能关注的文档
- ...智能Data Mining & Business Intelligence第三章 关联规则挖掘课件.ppt
- .opennet TechnologiesAn Introduction to XML课件.ppt
- 2017年度企业财务会计决算报表2018年度企业经济效益月报课件.ppt
- 2017年海南生物高考课件.ppt
- 2017年经管学情调查答题操作手册课件.ppt
- 2017高三数学备考科学路径与整体解决方案课件.ppt
- 2018届初三数学备考与复习思路课件.ppt
- 2018届高三上期末复习6课件.ppt
- 2018年公益项目(XXX项目)课件.ppt
- 2018年国家级科技计划项目跟踪调查统计填报说明课件.ppt
文档评论(0)