博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java入门之对象数组及集合概述
阅读量:4152 次
发布时间:2019-05-25

本文共 6295 字,大约阅读时间需要 20 分钟。

 对象数组

1.1 对象数组概述

A:基本类型的数组:存储的元素为基本类型    int[] arr={1,2,3,4}B:对象数组:存储的元素为引用类型    Student[] stus=new Student[3];Student代表一个自定义类    Stus数组中stus[0],stus[1],stus[2]的元素数据类型为Student,都可以指向一个Student对象

1.2 对象数组案例:

创建一个学生数组,存储三个学生对象并遍历

1.2.1 案例代码一:

package com.itheima;/* * 自动生成构造方法: *      代码区域右键 -- Source -- Generate Constructors from Superclass...    无参构造方法 *      代码区域右键 -- Source -- Generate Constructor using Fields...        带参构造方法 * 自动生成getXxx()/setXxx(): *      代码区域右键 -- Source -- Generate Getters and Setters... */public class Student {    private String name;    private int age;    public Student() {    }    public Student(String name, int age) {        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

.

package com.itheima;/* * 创建一个学生数组,存储三个学生对象并遍历 *  * 分析: *      A:定义学生类 *      B:创建学生数组 *      C:创建学生对象 *      D:把学生对象作为元素赋值给学生数组 *      E:遍历学生数组 */public class StudentDemo {    public static void main(String[] args) {        //创建学生数组        Student[] students = new Student[3];        //创建学生对象        Student s1 = new Student("曹操",40);        Student s2 = new Student("刘备",35);        Student s3 = new Student("孙权",30);        //把学生对象作为元素赋值给学生数组        students[0] = s1;        students[1] = s2;        students[2] = s3;        //遍历学生数组        for(int x=0; x
 
 
 
集合类之ArrayList 2.1	集合概述 A:我们学习的是面向对象编程语言,而面向对象编程语言对事物的描述都是通过对象来体现的。为了方便对多个对象进行操作,我们就必须对这多个对象进行存储,而要想对多个对象进行存储, 就不能是一个基本的变量,而应该是一个容器类型的变量。B:到目前为止,我们学习过了哪些容器类型的数据呢?    StringBuilder,数组。    StringBuilder的结果只能是一个字符串类型,不一定满足我们的需求。     所以,我们目前只能选择数组了,也就是我们前面学习过的对象数组。     但是,数组的长度是固定的, 如果有时候元素的个数不确定的,我们无法定义出数组的长度,这个时候,java就提供了集合类供我们使用。 2.2	ArrayList集合 2.2.1	ArrayList添加新元素 2.2.1.1	案例代码二: package com.itheima_01;import java.util.ArrayList;/* * 为什么会出现集合类: *      我们学习的是面向对象编程语言,而面向对象编程语言对事物的描述都是通过对象来体现的。 *      为了方便对多个对象进行操作,我们就必须对这多个对象进行存储,而要想对多个对象进行存储, *      就不能是一个基本的变量,而应该是一个容器类型的变量。 *      到目前为止,我们学习过了哪些容器类型的数据呢?StringBuilder,数组。 *      StringBuilder的结果只能是一个字符串类型,不一定满足我们的需求。 *      所以,我们目前只能选择数组了,也就是我们前面学习过的对象数组。 *      但是,数组的长度是固定的,适应不了变化的需求,那么,我们该如何选择呢? *      这个时候,java就提供了集合类供我们使用。 *  * 集合类的特点: *      长度可变。 *  * ArrayList
: * 大小可变数组的实现 * *
:是一种特殊的数据类型,泛型。 * 怎么用呢? * 在出现E的地方我们使用引用数据类型替换即可 * 举例:ArrayList
,ArrayList
* * 构造方法: * ArrayList() * * 添加元素: * public boolean add(E e):添加元素 * public void add(int index,E element):在指定的索引处添加一个元素 */public class ArrayListDemo { public static void main(String[] args) { //创建集合对象 ArrayList
array = new ArrayList
(); //add(E e):添加元素 array.add("hello"); array.add("world"); array.add("java"); //add(int index,E element):在指定的索引处添加一个元素 //array.add(1, "android"); System.out.println("array:"+array); }} 2.2.2 ArrayList删改查方法 A:获取元素 public E get(int index):返回指定索引处的元素B:集合长度 public int size():返回集合中的元素的个数C:删除元素 public boolean remove(Object o):删除指定的元素,返回删除是否成功 public E remove(int index):删除指定索引处的元素,返回被删除的元素D:修改元素 public E set(int index,E element):修改指定索引处的元素,返回被修改的元素 2.2.2.1 案例代码三: package com.itheima_01;import java.util.ArrayList;/* * 获取元素 * public E get(int index):返回指定索引处的元素 * 集合长度 * public int size():返回集合中的元素的个数 * 删除元素 * public boolean remove(Object o):删除指定的元素,返回删除是否成功 * public E remove(int index):删除指定索引处的元素,返回被删除的元素 * 修改元素 * public E set(int index,E element):修改指定索引处的元素,返回被修改的元素 */public class ArrayListDemo2 { public static void main(String[] args) { //创建集合对象 ArrayList
array = new ArrayList
(); //添加元素 array.add("hello"); array.add("world"); array.add("java"); //public E get(int index):返回指定索引处的元素 //System.out.println("get:"+array.get(0)); //System.out.println("get:"+array.get(1)); //System.out.println("get:"+array.get(2)); //public int size():返回集合中的元素的个数 //System.out.println("size:"+array.size()); //public boolean remove(Object o):删除指定的元素,返回删除是否成功 //System.out.println("remove:"+array.remove("world"));//true //System.out.println("remove:"+array.remove("world"));//false //public E remove(int index):删除指定索引处的元素,返回被删除的元素 //System.out.println("remove:"+array.remove(0)); //public E set(int index,E element):修改指定索引处的元素,返回被修改的元素 System.out.println("set:"+array.set(1, "android")); //输出 System.out.println("array:"+array); }} 2.2.3 ArrayList遍历 集合的遍历思想和数组的遍历思想相同循环遍历容器,依次取出里面的元素即可 2.2.3.1 案例代码四: package com.itheima_01;import java.util.ArrayList;/* * ArrayList集合的遍历 * 通过size()和get()配合实现的 */public class ArrayListDemo3 { public static void main(String[] args) { //创建集合对象 ArrayList
array = new ArrayList
(); //添加元素 array.add("hello"); array.add("world"); array.add("java"); //获取元素 //原始做法 System.out.println(array.get(0)); System.out.println(array.get(1)); System.out.println(array.get(2)); System.out.println("----------"); for(int x=0; x<3; x++) { System.out.println(array.get(x)); } System.out.println("----------"); //如何知道集合中元素的个数呢?size() for(int x=0; x
array = new ArrayList
(); //添加字符串元素 array.add("向问天"); array.add("刘正风"); array.add("左冷禅"); array.add("风清扬"); //遍历集合 for(int x=0; x
array = new ArrayList
(); //遍历字符串数组,获取到每一个字符串元素 for(int x=0; x
array = new ArrayList
(); //创建学生对象 Student s1 = new Student("林青霞",28); Student s2 = new Student("张曼玉",30); Student s3 = new Student("景甜",25); Student s4 = new Student("柳岩",18); //把学生对象作为元素添加到集合中 array.add(s1); array.add(s2); array.add(s3); array.add(s4); //遍历集合 for(int x=0; x
array = new ArrayList
(); /* //键盘录入数据,创建学生对象,把键盘录入的数据赋值给学生对象的成员变量 Scanner sc = new Scanner(System.in); System.out.println("请输入学生姓名:"); String name = sc.nextLine(); System.out.println("请输入学生年龄:"); String age = sc.nextLine(); Student s = new Student(); s.setName(name); s.setAge(age); //把学生对象作为元素存储到集合中 array.add(s); */ //为了提高代码的复用性,我把键盘录入数据给学生对象,并存储到集合中的动作用一个方法来实现 //调用方法 addStudent(array); addStudent(array); addStudent(array); //遍历集合 for(int x=0; x
array */ public static void addStudent(ArrayList
array) { //键盘录入数据,创建学生对象,把键盘录入的数据赋值给学生对象的成员变量 Scanner sc = new Scanner(System.in); System.out.println("请输入学生姓名:"); String name = sc.nextLine(); System.out.println("请输入学生年龄:"); String age = sc.nextLine(); Student s = new Student(); s.setName(name); s.setAge(age); //把学生对象作为元素存储到集合中 array.add(s); }}
 

转载地址:http://falti.baihongyu.com/

你可能感兴趣的文章
Openwrt源码下载与编译
查看>>
我和ip_conntrack不得不说的一些事
查看>>
Linux 查看端口使用情况
查看>>
文件隐藏
查看>>
两个linux内核rootkit--之二:adore-ng
查看>>
两个linux内核rootkit--之一:enyelkm
查看>>
关于linux栈的一个深层次的问题
查看>>
rootkit related
查看>>
配置文件的重要性------轻化操作
查看>>
又是缓存惹的祸!!!
查看>>
为什么要实现程序指令和程序数据的分离?
查看>>
我对C++ string和length方法的一个长期误解------从protobuf序列化说起(没处理好会引起数据丢失、反序列化失败哦!)
查看>>
一起来看看protobuf中容易引起bug的一个细节
查看>>
无protobuf协议情况下的反序列化------貌似无解, 其实有解!
查看>>
make -n(仅列出命令, 但不会执行)用于调试makefile
查看>>
makefile中“-“符号的使用
查看>>
go语言如何从终端逐行读取数据?------用bufio包
查看>>
go的值类型和引用类型------重要的概念
查看>>
求二叉树中结点的最大值(所有结点的值都是正整数)
查看>>
用go的flag包来解析命令行参数
查看>>