您的位置:首页 > 编程语言 > Java开发

JAVA高级应用之集合 泛型 的使用

2018-01-22 21:24 525 查看

JAVA高级应用之集合

泛型

基础类

Person类

package com.lanou3g.bean;
/*
* 姓名 和 年龄
* 构造 set\get toString
* 创建一个学生类(构造方法 toString) 继承 Person类
*/
public class Person {
private String name;
private int age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}

}


学生类继承Person类

package com.lanou3g.bean;

public class Student extends Person{
public Student() {
super();
// TODO Auto-generated constructor stub
}

public Student(String name, int age) {
super(name, age);
// TODO Auto-generated constructor stub
}
}


worker类

public class Worker<W>{
//  利用泛型 写成员变量
private W w;
//  利用泛型写set\get方法
public void setW(W w){
this.w = w;
}
public W getW(){
return this.w;
}
//  普通成员方法
public void sayHi(W w){
System.out.println(w);
}
//  工作
public void work(){
System.out.println("天天搬砖  一次搬400块");
}
//  声明不同泛型的方法 在调用泛型的时候 指定泛型的类型
//  <Z>声明一个泛型 只有声明过的 才能使用
public<Z> void Printable(Z z){
System.out.println(z);
}

//  静态方法中 能不能使用 W
//  当你调用静态方法的时候 还能还没有对象 没有对象就没有指定泛型
public static void fun(Q q){   //错误写法
System.out.println(q);
}
public<Q> static void fun(Q q){   //正确写法
System.out.println(q);
}

}


测试类

package com.lanou3g.bean;

public class Test {
public static void main(String[] args) {
Worker<String> worker = new Worker<String>();
worker.sayHi("wwww");
worker.Printable(2345);
Worker.fun("q");
}
}


泛型接口

public interface InterA<Y>{
public abstract void fun(Y y);
}

class InterAImpl implements InterA<String>{
@Override
public void fun(String y){
System.out.println(y);
}
}


代码解释泛型

泛型:表示集合中 保存数据的类型
要求:能看懂就行


保存字符串

创建一个集合 保存 abcd
<E> E就代表要保存的元素类型
后面的尖括号 要跟前面填写的泛型 保持一致
jdk 1.7出来的 菱形泛型
如果前面声明了泛型 后面泛型可以省略不写


public static void fun(){
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");

Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
//  添加泛型后 可以省去强制转换的麻烦
System.out.println(iterator.Next())
}
}


保存三个学生

public static void fun2(){
ArrayList<Student> arrayList = new ArrayList<Student>;
arrayList.add(new Student("刘德华",15));
arrayList.add(new Student("刘亦菲",16));
arrayList.add(<
e01d
span class="hljs-keyword">new Student("刘天宇",17));

Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
Student student = iterator.Next();
System.out.println(student);
}
}


创建泛型类

public static void fun3(){
//  创建泛型类
Worker<String> worker = new Worker<>();
worker.setW("哈哈");
System.out.println(worker.getW());
//  调用成员方法
worker.sayHi("哈哈哈哈");
//  调用方法 给定泛型
woeker.Printable("你好");
//  调用静态方法
Worker.fun("石头人");
}


范围限定

? extends E (向下限定)
? 是子类 继承 E 是父类 ?号只能是E类的子类
? super E (向上限定)
? 是 父类 E是子类 ?只能是E类的 父亲


示例1

创建一个保存人的集合 存两人
创建一个保存学生的集合 存两人

public static void fun1(){
ArrayList<Person> arrayList = new ArrayList<Person>();
arrayList.add(new Person("刘德华",15));
arrayList.add(new Person("刘亦菲",16));

ArrayList<Student> arrayList1 = new ArrayList<Student>();
arrayList1.add(new Student("刘邦",30));
arrayList1.add(new Student("项羽",50));

//  学生的集合全部添加到人的集合中
arrayList.addAll(arrayList1);
System.out.println(arrayList);
//  ? extends Person  只能将Person的子类添加到Person中
//  错误写法
arrayList.addAll(arraylist);
}


示例2

int ... num 相当于传入的参数是个数组
int ... num 可以接受多个参数 只能是方法参数的最后一个
jdk 1.5出来的


public static void fun3(){
int[] array = {1,2,4,3,8};
fun2(10,array);
fun2(1,2,3,4,5,6);
}

public static void fun2(int j ,int ... num){
for(int i = 0; i < num.length(); i++){
System.out.println(num[i]);
}

System.out.println(j);
}


数组转集合

Arrays中的方法 把数组转为集合
使用asList数组转集合 得到一个集合
注意:这个集合不允许添加和删除操作
这么转的目的是为了调用集合中的其他方法


int[] array = {1,2,3,4,5};   //没有进行自动装箱
//  把数组当做集合中的一个元素 转为了集合
List<int[]> asList = Arrays.asList(array);
System.out.println(asList);  //输出:地址

Interger[] array2 = {1,2,3,4,5}; //自动装箱为Integer类型
List<Integer> asList2 = Arrays.asList(array2);
System.out.println(asList2);   //输出: 1 2 3 4 5

String[] array3 = {"刘德华","刘亦菲"};
List<String> asList3 = Arrays.asList(array3);
asList.add("刘天宇");   // 错误写法
boolean isContain = asList3.contains("刘德华");
asList.set(0,"刘邦");
System.out.println(asList3);  //输出结果:刘邦  刘亦菲


集合中的删除(掌握)

不使用迭代器 遍历

不使用迭代器 遍历   创建集合 保存 a b c d e

public static void fun1(){
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");

for(int i = 0; i < arrayList.size(); i++){
if(arrayList.get(i).equals("b")){
arrayList.remove(i--);
}
}
System.out.println (arrayList);
}


迭代器删除

public static void fun2(){
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arraylist.add("c");
arrayList.add("d");

Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
if(iterator.Next().equals("b")){
lterator.remove();
}
}
System.out.println(arrayList);
}


利用增强for循环打印

public static void fun3(){
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");

for(String string : arrayList){
System.out.println(string);
}
}


创建 一个集合 存入5个学生 按学生年龄 进行排序

public static void fun(){
public static void main(String[] args){
ArrayList<Student> arrayList = new ArrayList<Student>();
arrayList.add(new Student("刘德华",15));
arrayList.add(new Student("刘亦菲",16));
arrayList.add(new Student("刘天宇",17));

for(int i= 0; i < arrayList.size() - 1; i++){
for(int j = 0; j < arrayList.size() - i - 1; j++){
Student s1 = arrayList.get(j);
Student s2 = arrayList.get(j + 1);
if(s1.getAge() > s2.getAge()){
arrayList.set(j , s2);
arrayList.set(j + 1, s1);
}
}
}

System.out.println(arrayList);
}
}


按姓名比较

public static void sortByName(ArrayList<Student> arrayList){
public static void main(String[] args){

for(int i = 0; i < arrayList.size() - 1; i++){
for(int j = 0; i < arrayList.size() - i - 1; j++){
Student s1 = arrayList.get(j);
Student s2 = arrayList.get(j + 1);

if(s1.getName().compareTo(s2.getName()) > 0){
Collection.swap(list, j, j + 1);
}
}
}

}

}


代码示例

* 创建一个集合保存 java学科
* 有2个班 班里有学生
* 遍历java学科中的学生


public class Demo05{
public static void main(String[] args){
ArrayList<ArrayList<Student>> javaProject = new ArrayList<ArrayList<Student>>();

ArrayList<Student> class1 = new ArrayList<Student>();
ArrayList<Student> class2 = new ArrayList<Student>();

class1.add(new Student("刘德华",15);
class1.add(new Student("刘亦菲",16));
class2.add(new Student("胡歌",17));
class2.add(new Student("王龙",18);

for(ArrayList<Student> arrayList : javaProject){
for(Student student : arrayList){
System.out.println(student);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐