package Day08;
import java.util.ArrayList;
/**
* 二
* 字符串数组strs中包含字符串{"12","345","6789","1","123","4567"},
* 创建2个ArrayList集合,
* 遍历strs数组,并判断数组中每一个元素的长度,
* 将长度为偶数的元素和长度为奇数的元素分别存放在两个集合中,
* 最终将这两个集合在控制台打印输出:
*/
public class Day08_Demo02 {
public static void main(String[] args) {
String[] strs = {"12", "345", "6789", "1", "123", "4567"};
ArrayList<String> oslist = new ArrayList<>();//偶数list
ArrayList<String> jslist = new ArrayList<>();//奇数list
for (int i = 0; i < strs.length; i++) {
//遍历数组元素的长度,偶数 和奇数,放入不同的list集合
if (strs[i].length() % 2 == 0) {
oslist.add(strs[i]);//偶数
} else {
jslist.add(strs[i]);//奇数
}
}
System.out.println("-------------输出偶数list----------------");
for (String s : oslist) {
System.out.println(s+" 元素个数为 "+s.length());
}
System.out.println("-------------输出奇数list----------------");
for (String s:jslist) {
System.out.println(s+" 元素个数为 "+s.length());
}
}
}
java基础习题练习