zoukankan      html  css  js  c++  java
  • Java 对象数组

    java对象数组的概述和使用

     1 public class Student
     2 {
     3     // 成员变量
     4     private String name;
     5     private int age;
     6 
     7     // 构造方法
     8     public Student()
     9     {
    10         super();
    11     }
    12 
    13     public Student(String name, int age) 
    14     {
    15         super();
    16         this.name = name;
    17         this.age = age;
    18     }
    19 
    20     // 成员方法
    21     // getXxx()/setXxx()
    22     public String getName() 
    23     {
    24         return name;
    25     }
    26 
    27     public void setName(String name) 
    28     {
    29         this.name = name;
    30     }
    31 
    32     public int getAge() 
    33     {
    34         return age;
    35     }
    36 
    37     public void setAge(int age) 
    38     {
    39         this.age = age;
    40     }
    41 
    42     @Override
    43     public String toString() 
    44     {
    45         return "Student [name=" + name + ", age=" + age + "]";
    46     }
    47 }
     1 /**
     2 把5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
     3 *         学生:Student
     4 *         成员变量:name,age
     5 *         构造方法:无参,带参
     6 *         成员方法:getXxx()/setXxx()
     7 * 分析:
     8 *         A:创建学生类。
     9 *         B:创建学生数组(对象数组)。
    10 *         C:创建5个学生对象,并赋值。
    11 *         D:把C步骤的元素,放到数组中。
    12 *         E:遍历学生数组。
    13 *  */
    14 
    15 public class Practice 
    16 {
    17     public static void main(String[] args)
    18     {
    19         // 创建学生数组(对象数组)。
    20         Student[] students = new Student[5];
    21         // for (int x = 0; x < students.length; x++) 
    22         // {
    23         //         System.out.println(students[x]);
    24         // }
    25         //     System.out.println("---------------------");
    26 
    27         // 创建5个学生对象,并赋值。
    28         Student s1 = new Student("小明", 27);
    29         Student s2 = new Student("小红", 30);
    30         Student s3 = new Student("小强", 30);
    31         Student s4 = new Student("旺财", 12);
    32         Student s5 = new Student("张三", 35);
    33 
    34         // 将对象放到数组中。
    35         students[0] = s1;
    36         students[1] = s2;
    37         students[2] = s3;
    38         students[3] = s4;
    39         students[4] = s5;
    40 
    41         // 遍历
    42         for (int x = 0; x < students.length; x++) 
    43         {
    44             //System.out.println(students[x]);
    45             Student s = students[x];
    46             System.out.println(s.getName()+"---"+s.getAge());
    47         }
    48     }
    49 }

    15.02 对象数组的内存图解

    15.03 集合的由来及与数组的区别

    集合类的由来:面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。

    数组和集合类同的区别:

    数组可以存储同一种类型的基本数据也可以存储同一种类型的对象,但长度是固定的

    集合只可以存储不同类型的对象,长度是可变的

    集合类的特点:集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。

    15.04 集合的继承体系图解

    集合容器因为内部的数据结构不同,有多种具体容器,根据共性内容不断的向上抽取,就形成了集合框架。

    框架的顶层Collection接口

    15.05 Collection集合的功能概述

    Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。

    15.06 Collection集合的基本功能测试

    成员方法:

    1.  boolean add(Ee):确保此 collection 包含指定的元素(可选操作)。

    2.  boolean remove(Objecto):从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。

    3.  void clear():移除此 collection 中的所有元素(可选操作)。

    4.  boolean contains(Objecto):如果此 collection 包含指定的元素,则返回 true。

    5.  boolean isEmpty():如果此 collection 不包含元素,则返回 true。

    6.  int size():返回此 collection 中的元素数。

    例:

     1 // 创建集合对象
     2 // Collection c = new Collection(); //错误,因为接口不能实例化
     3 Collection c = new ArrayList();
     4 c.add("hello");
     5 c.add("world");
     6 c.add("java");
     7 // c.clear();//移除所有元素
     8 // System.out.println("remove:" + c.remove("hello"));//移除一个元素
     9 // System.out.println("remove:" + c.remove("javaee"));
    10 // 判断集合中是否包含指定的元素
    11  System.out.println("contains:"+c.contains("hello"));//contains:true
    12  System.out.println("contains:"+c.contains("android"));//contains:false
    13  //判断集合是否为空
    14  System.out.println("isEmpty:"+c.isEmpty());//isEmpty:false
    15 //元素的个数
    16 System.out.println("size:"+c.size());//size:3
    17 System.out.println("c:" + c);//c:[hello, world, java]

    15.07 Collection集合的高级功能测试

    成员方法:

    1.  boolean addAll(Collection<? extends E> c):

    将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。

    2.  boolean removeAll(Collection<?> c):

    移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。

    3.  boolean containsAll(Collection<?> c):

    如果此 collection 包含指定 collection 中的所有元素,则返回 true。

    4.  boolean retainAll(Collection<?> c):

    仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。换句话说,移除此 collection 中未包含在指定 collection 中的所有元素。

    例:

    c1.addAll(c2);//将c2集合中的所有元素添加到c1集合中,c1变c2不变
    c1.removeAll(c2);//将c1集合中与c2集合相同的所有元素删除,只要有一个相同的就返回true
    c1.containsAll(c2);//判断c1集合中的元素是否包含c2中的全部元素,全部包含则返回true
    c1.retainAll(c2);//将c1集合中与c2集合相同的元素保留,删除其他元素,返回值表示c1集合是否发生变化,发生变化返回true,没有变化返回false

    15.08 集合的遍历之集合转数组遍历

    Object[] toArray():返回包含此 collection 中所有元素的数组。

    例:

     1 public class Practice 
     2 {
     3     public static void main(String[] args)
     4     {
     5         // 创建集合
     6         Collection c = new ArrayList();
     7         c.add("hello");
     8         c.add("world");
     9         c.add("java");
    10         
    11         Object[] objs = c.toArray();
    12         for (int i = 0; i < objs.length; i++) 
    13         {
    14             //向下转为String类型
    15             String s = (String)objs[i];
    16             System.out.println(s+":"+s.length());
    17         }
    18     }
    19 }

    运行结果:

    hello:5
    world:5
    java:4
  • 相关阅读:
    全面了解 NOSQL
    金融业容灾技术分析
    银行业务知识(转)
    结合工作的业务连续性实践
    金融企业架构
    window 下拉取github项目失败 (Permission denied (publickey))
    vsftpd 配置文件
    nginx下配置虚拟主机
    linux 下安装ftp 并远程连接
    find_in_set
  • 原文地址:https://www.cnblogs.com/dhm520/p/8530323.html
Copyright © 2011-2022 走看看