java集合类collection的用法(二)

约定不等于承诺〃 2022-05-26 00:18 176阅读 0赞
  1. public class Text10{
  2. public static void main(String[] args) {
  3. //集合一
  4. //创建集合对象
  5. Collection c = new ArrayList();
  6. c.add("你好");
  7. c.add("加油");
  8. //集合二

// Collection c1 = new ArrayList();
c.add(“努力”);
c.add(“勤奋”);
//Object objs =”hello” 向上转型
//object []toArray();把集合转换数组,可以实现集合的遍历
Object []objs =c.toArray();
for (int i = 0; i < objs.length; i++) {
String s = (String) objs[i];
System.out.println(s+”===”+s.length());
}
// System.out.println(Object[i]);
//我知道元素是字符串,我在获取到元素的同时,害羞真的元素的长度
// System.out.println(objs[i]+”===”+objs[i].length());
//上面的实现不了,原因是Object中没有length()方法
// 我们想使用字符串的方法,就必须能把元素还原字符串
集合类Collection的遍历数组
public class Student {

  1. private String name;
  2. private int age;
  3. public Student(String string, int i) {
  4. super();
  5. this.age =age;
  6. this.name=name;
  7. }
  8. public static void main(String[] args) {
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public void setAge(int age) {
  14. this.age = age;
  15. }
  16. public int getAge() {
  17. return age;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public class Text10{
  23. public static void main(String[] args) {
  24. //集合一
  25. //创建集合对象
  26. Collection c = new ArrayList();
  27. //创建学生对象
  28. Student s1 = new Student("小明",22);
  29. Student s2 = new Student("小红",23);
  30. Student s3 = new Student("小花",25);
  31. Student s4 = new Student("小宇",25);
  32. Student s5 = new Student("小伟",27);
  33. //把学生类添加到集合
  34. c.add(s1);
  35. c.add(s2);
  36. c.add(s3);
  37. c.add(s4);
  38. c.add(s5);
  39. //把集合转换数组
  40. Object []objs =c.toArray();
  41. //遍历数组
  42. for (int i = 0; i < objs.length; i++) {
  43. Student s = (Student) objs[i];
  44. System.out.println(s.getName()+"==="+s.getAge());
  45. }

public class Text10{
//获取元素 Iterator的用法
// Object next()
public static void main(String[] args) {
//集合一
//创建集合对象
Collection c = new ArrayList();
//添加元素
c.add(“你好”);
c.add(“加油”);
c.add(“努力”);
c.add(“勤奋”);

  1. //Iterator iterator() 迭代器,集合的遍历方式
  2. /* boolean hasNext() 如果仍有元素可以迭代,则返回 true。
  3. * E next() 返回迭代的下一个元素。
  4. * void remove() 从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。
  5. * 获取元素时先判断,再获取 否则会报错
  6. */
  7. Iterator it =c.iterator();
  8. //hasNext的用法 获取所以元素
  9. while (it.hasNext()) {
  10. String s =(String)it.next();
  11. System.out.println(s);
  12. }
  13. //next的用法 获取一个元素
  14. Object obj = it.next();
  15. System.out.println(obj);
  16. //获取多个 需要加上判断
  17. if (it.hasNext()) {
  18. System.out.println(it.hasNext());
  19. }
  20. if (it.hasNext()) {
  21. System.out.println(it.hasNext());
  22. }
  23. if (it.hasNext()) {
  24. System.out.println(it.hasNext());
  25. }

List的遍历用法
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

  1. public class Text10{
  2. public static void main(String[] args) {
  3. //集合一
  4. //创建集合对象
  5. Collection c = new ArrayList();
  6. List list = new ArrayList();
  7. //添加元素
  8. c.add("你好");
  9. c.add("加油");
  10. c.add("努力");
  11. c.add("勤奋");
  12. Iterator it =c.iterator();
  13. //hasNext的用法 获取所以元素
  14. while (it.hasNext()) {
  15. String s =(String)it.next();
  16. System.out.println(s);
  17. }

发表评论

表情:
评论列表 (有 0 条评论,176人围观)

还没有评论,来说两句吧...

相关阅读