java0526

String

new String(“abc")

s1 == s2   false
s1.equals(s2)    true

image-20240526093701687

String s = "abc";

包装类型Integer

        Integer a = 256;
        Integer b = 256;
​
        System.out.println(a == b);

image-20240526094859928

String s1 = "a";
String s2 = "b";
String s3 = "ab";
boolean f1 = (s1 + s2) == s3;
boolean f2 = ("a" + "b") == "ab";
boolean f3 = (s1 + "b") == s3;
​
System.out.println("f1=" + f1);  //true
System.out.println("f2=" + f2);   //true
System.out.println("f3=" + f3);   //false

String s = new String( "abc");

String s1 = "abc";

java集合

存储多个数据

为什么使用集合

数组:

int[] a = {1,2,3,34};

缺点:

长度固定的,类型固定,不好检测。

常用集合

接口: Collection

image-20240526103821566

image-20240526104014715

List接口

ArrayList, LinkedList, Vector

ArrayList

image-20240526104421243

add()
remove()
set(index, 值)
get()
  1. for

  2. foreach

toString()

删除的问题

迭代器方案

image-20240526111541051

image-20240526111729047

Iterator<Integer> it = list.iterator(); //拿到迭代器
​
while(it.hasNext()){ //判断是否有下个
    int n = it.next(); //拿到下一个
    //System.out.print(n+"\t");;
    if(n%3==0|| n%5==0){
        it.remove();
    }
}
System.out.println(list);

ArrayList

底层是数组,大小是固定。

思路: 创建新数组,原来容量1.5,拷贝数据

创建对象

 List<Student> list
                =new ArrayList<>();

image-20240526134726054

每次扩容的是原大小1.5被

ArrayList初始值大小:10

image-20240526135234385

设置初始值,目的是减少扩容次数

image-20240526135624773

api

add()

remove()

删除对象,要重写equals方法。

image-20240526141101039

set(index, obj)

LinkedList

查询修改效率低

删除·,添加效率高

任务,定义个LinkedList集合,放入随机(1~100)产生的20个整数。

set

有一个List集合存入20个数据,随机产生,不能保证数据不重复,排除所有重复数据。

Set集合不能保证顺序。

元素中 equals()和hashcode()

Set<Integer> r = new HashSet<>();
Random random = new Random();
while (r.size() < 20) {
    int n = random.nextInt(20) + 1;
    r.add(n);
}
System.out.println("set:" + r);

有一个list集合,有若干数据有重复的数据,去掉重复数据。

Stack

1+2+3-5+5+8

假设只能输入+ - ,

LIFO(后进先出)

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值