- 分析以下需求,并用代码实现:
(1)有如下代码:
(2)定义方法统计集合中指定元素出现的次数,如"a" 3,"b" 2,"c" 1
List<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
System.out.println(frequency(list, "a")); // 3
System.out.println(frequency(list, "b")); // 2
System.out.println(frequency(list, "c")); // 1
System.out.println(frequency(list, "d")); // 5
System.out.println(frequency(list, "xxx")); // 0
-
分析以下需求,并用代码实现:
(1)生成10个1至100之间的随机整数(不能重复),存入一个List集合
(2)编写方法对List集合进行排序
(2)然后利用迭代器遍历集合元素并输出
(3)如:15 18 20 40 46 60 65 70 75 91 -
分析以下需求,并用代码实现:
(1)定义List集合,存入多个字符串,其中包含三个连续的"def"
(2)删除集合中字符串"def"
(3)然后利用迭代器遍历集合元素并输出 -
分析以下需求,并用代码实现:
(1)定义List集合,存入多个字符串
(2)删除集合中元素(只要字符串中包含z或者是m中的任意一个字符就需要删除此整个字符串)
(3)然后利用迭代器遍历集合元素并输出
1、
public static int frequency(List<String> list, String s) {
//定义一个变量i来统计次数
int i = 0;
for (String str : list) {
if (str.equals(s)) {
i++;
}
}
return i;
}
2、
package com.xxx;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Exe2 {
public static void main(String[] args) {
//生成10个1至100之间的随机整数(不能重复),存入一个List集合
List list = new ArrayList();
for (int i = 0; i < 10; i++) {
//随机数num
int num = (int) (Math.random()*100+1);
int m = 0;
for (int j = 0; j < list.size(); j++) {
int o = (int) list.get(j);
if (o == num) {
m++;
}
}
if (m == 0) {
list.add(num);
}
}
System.out.println(list);
Bubbling(list);
Test(list);
}
//排序
public static List Bubbling(List list) {
Collections.sort(list);
return list;
}
//迭代器遍历集合并输出
public static void Test(List list) {
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
int i = (int) iterator.next();
System.out.print(i + " ");
}
}
}
3、
package com.xxx;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Exe3 {
public static void main(String[] args) {
List list = new ArrayList();
String[] strings = {"de", "ef", "def", "def", "def", "fd"};
for (String s : strings) {
list.add(s);
}
System.out.println(list);
//删除集合字符串:"def"
//方法一:不推荐
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("def")) {
list.remove(i);
//移除元素后数组长度会减一
i--;
}
}
//方法二:倒序删
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i).equals("def")) {
list.remove(i);
}
}
//方法三:迭代器删除(推荐)
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
String s = (String) listIterator.next();
if (s.equals("def")) {
listIterator.remove();
}
}
//遍历输出
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + "\t");
}
}
}
4、
package com.xxx;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Exe4 {
public static void main(String[] args) {
List list = new ArrayList();
String[] ss = {"a", "b", "c", "z", "m", "zm", "d"};
for (String s : ss) {
list.add(s);
}
System.out.println(list);
//删除集合中指定元素
for (int i = 0; i < list.size(); i++) {
String s = (String) list.get(i);
if (s.contains("m") || s.contains("z")) {
list.remove(i);
i--;
}
}
System.out.println(list);
//遍历输出
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + "\t");
}
}
}
标签:练习题,String,iterator,int,06List,list,add,List
From: https://www.cnblogs.com/wyzel/p/16794380.html