java学习:常见数据结构1

1:常见数据结构

栈:先进后出
队列:先进先出
数组:查询快,增删慢
链表:查询慢,增删快(对比数组) 都要从头(head)开始查询
节点包含数据和下一个结点的地址
头节点:head+^ :节点指向空地址,表示结束

2:List集合子类特点

常用子类:ArrayList,LinkedList
Arraylist:查询快,增删慢;LinkedList:查询慢,增删快
使用ArrayList,LinkedList完成存储字符并遍历

public class listDemo{
    public static void main(String[] args){
        //创建集合对象
        ArrayList<String> array = new ArrayList<String>();
        
        array.add("hello");
        array.add("world");
        //遍历(迭代器,普通for,增强for)
        for(String s : array){
            System.out.println(s);
        }

        LinkedList<String> linkedList = new ArrayList<String>();

        linkedList.add("hello");
        linkedList.add("world");

        for(String s : linkedList){
            System.out.println(s);
        }

    }
}

3:三种遍历方式:

迭代器:集合特有的遍历方式
普通for:带有索引的遍历方式
增强for:最方便的遍历方式

//迭代器
Iterator<Student> it = array. iterator();
while (it .hasNext()){
    Student s = it.next();
    System.out.println(s. getName() + "," + s.getAge());
}
System.out.println--------");

//普通for
for(int i=0; i<array.size(); i++) {
    Student s = array.get(i);
    System.out.println(s. getName() + "," + s.getAge());
}
System.out.println--------");

//增强for
for(Student s : array) {
    System.out.printin(s.getName() + "," + s.getAge()); 
}

4:LinkedList集合的特有功能

public void addFirst(E e) 在该列表开头插入指定的元素
public void addLast(E e) 将指定的元素追加到此列表的末尾
public E getFirst( ) 返回此列表中的第一个元素
public E getLast( ) 返回此列表中的最后一个元素
public E removeFirst( ) 从此列表中删除并返回第一个元素
public E removeLast( ) 从此列表中删除并返回最后一个元素

LinkedList.addFirst("hello");
LinkedList.addLast("hello");
LinkedList.getFirst( );
LinkedList.getLast( );
LinkedList.removeFirst( );
LinkedList.removeLast( );

5:set集合

特点:
不包含重复元素,不能使用普通for循环遍历

创建集合对象并遍历:

HashSet:对集合的迭代顺序不做保证

Set<String> set = new HashSet<String>();
set.add("hello");
//遍历
for(String s : set){
    System.out.println(s);
}

哈希值:
根据对象的地址或者字符串或者数字算出来的int类型的数值
获取哈希值:
public int hashCode();

Student s1 = new Student(name:"jake",age:"12");
int s=s1.hashCode();
System.out.println(s);

//同一个对象多次调用hashCode()返回的哈希值是一样的
//默认情况下,不同对象的哈希值是不一样的,可通过方法重写使哈希值相同

6 :HashSet集合特点:

·底层数据结构是哈希表
·对集合的迭代顺序不做保证
·不包含重复元素,不能使用普通for循环遍历

HashSet创建集合对象并遍历:

HashSet<String> set = new HashSet<String>();
set.add("hello");
//遍历
for(String s : set){
    System.out.println(s);
}

LinkedHashSet集合

·哈希值和链表实现的set接口,具有可预测的迭代顺序
·元素的存储和取出顺序是一致的
·没有重复元素

LinkedHashSet创建集合对象并遍历:

LinkedHashSet<String> set = new LinkedHashSet<String>();
set.add("hello");
//遍历
for(String s : set){
    System.out.println(s);
}

7 :TreeSet集合特点:

·不包含重复元素
·不能使用普通for循环遍历
·元素有序,取决于构造方法
TreeSet( );根据元素的自然排序进行排序,从小到大的顺序
TreeSet(Comparator comparator):根据指定的比较器排序

TreeSet存储整数并遍历:

TreeSet<Integer> set = new TreeSet<Integer>();
set.add(14);
set.add(34);
//遍历
for(Integer i : set){
    System.out.println(i);
}

自然排序comparable的使用

//创建学生类
public class Student implements Comparable<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; }
    @Override
    public int compareTo(Student s){
        return 0;//返回1则升序,返回-1则降序
    }
}
public class TreeSetDemo {
    public. static void main(String[] args) {
        //创建集合对象
        TreeSet<Student> ts = new TreeSet<Student>();
        //创建学生对象
        Student s1 = new Student( name: "小明", age: 29) ;
        Student s2 = new Student( name: "小红", age: 28);
        Student s3 = new Student( name: "小兰", age: 30);
        Student s4 = new Student( name: "小芳", age: 33);
        
        ts.add(s1);
        ts.add(s2);
        ts. add(s3);
        ts. add(s4);
        //遍历
        for (Student s : ts) {
            System. out . print1n(s. getName() + "," + s.getAge());
        }
    }
}

比较器comparator的使用:

TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){
    public int compare(Student s1,Student s2){
        int num = s1.getAge()- s2.getAge();
        int num2 =num == 0 ? s1.getName().compareTo( s2.getName()) : num;
        return num2;
    }
});

//创建学生对象
Student s1 = new Student( name: "xishi", age: 29) ;
Student s2 = new Student( name: "wangzhaojun", age: 28);
        
ts.add(s1);
ts.add(s2);
//遍历
for (Student s : ts) {
    System. out . print1n(s. getName() + "," + s.getAge());
}

8:泛型类:

定义格式:修饰符 class 类名<类型>{ }
public class Generic{ } 常见的T K E V等形式的参数常用于表示泛型

public class Generic<T>{
    private T t;
    public T getT() {
        return t;
    }
    public void setT(T  t) {
        this.t = t;
    }
}
Generic<String> g1 = new Generic<String>();
g1.setT("lily");
System.out.println(g1.getT());
Generic<Intrger> g2 = new Generic<Intrger>();
g2.setT(30);
System.out.println(g2.getT());

泛型的方法:

格式 : 修饰符<类型>返回值类型 方法名(类型变量名){ }
范例 : public void show(T t){ }

泛型接口
格式 : 修饰符 interface 接口名<类型>{ }
范例 : public interface Generic{ }

类型通配符:
●<?>
●List<?>: 表示元素类型末知的List,它的元素可以匹配任何的类型
●这种带通配符的List仅表示它是各种泛 型List的父类,并不能把元素添加到其中

●类型通配符 上限: <? extends类型>
●List<? extends Number>:它表示的类型是Number或者其子类型

●类型通配符 下限: <?super 类型>
●List<? super Number>:它表示的类型是Number或者其父类型

//类型通配符: <?>
List<?> list1 = new ArrayList<object>();
List<?> list2 = new ArrayList <Number>();
List<?> list3 = new ArrayList<Integer>();
System. out . printl1n(------");

//类型通配符上限: <? extends 类型>
List<? extends Number> list5 = new ArrayL ist<Number>();
List<? extends Number> list6 = new ArrayList<Integer>();
System. out . print1-(------");

//类型通配符下限: <? super 类型>
List<? super Number> list7 = new ArrayList<object>();
List<? super Number> list8 = new ArrayList<Number>();

可变参数:
格式 : 修饰符 返回值类型 方法名 (数据类型… 变量名){ }
范例 :
public static int sum(int…a){ } //a为数组
public static int sum(int b,int…a){ } //可变参数放在最后

System. out . println(sum( ...a:10, 20, 30, 40, 50, 60 ));
System. out . println(sum( ...a:10, 20, 30, 40, 50, 60, 70 ));
public static int sum(int... a) {
    int sum = 0;
    for(int i : a) {
        sum += i;
    }
    return sum;
}

可变参数的使用

Arrays类中有一个静态方法: .
● public static List aslist(… a) : 返回由指定数组支持的固定大小的列表
● 返回的集合不能做增删操作,可以做修改操作

*List接口中有一个静态方法: *
● public static List of(E… elements) : 返回包含任意数量元素的不可变列表
● 返回的集合不能做增删改操作

Set接口中有一个静态方法:
● public static Set of(… elements) : 返回-一个包含任意数量元素的不可变集合
● 给元素不能给重复元素
● 返回的集合不能做增删操作,没有修改的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安然907

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值