Collection、迭代器(Iterator)一些知识+数组删除指定元素并向前移动

1.用Collection存储自定义对象(Student),并遍历。删除名字是"张三"的学生。

package com.secondphase.homework.day01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @Author jinman1012@qq.com   2020/8/11 19:22
 * @Version 1.0
 */
public class Problem2 {
    public static void main(String[] args) {
        Student stu1 = new Student("张三",24);
        Student stu2 = new Student("李四",25);
        Student stu3 = new Student("王五",24);
        Collection collection = new ArrayList();
        collection.add(stu1);
        collection.add(stu2);
        collection.add(stu3);
        System.out.println(collection);
        for(Iterator iterator = collection.iterator();iterator.hasNext();){
            Student stu = (Student) iterator.next();
            if("张三".equals(stu.getName()))
                iterator.remove();
        }
        System.out.println(collection);
    }
}
class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

在这里插入图片描述

  1. 给定一个数组nums和一个值 val,你需要原地移除所有数值等于val的元素,并讲数组中不等val的值移动到数组的前面。返回移除后不等于val的元素个数。
    注意:
    原地的意思是空间复杂度为O(1),也就是在原数组上进行修改。元素的顺序可以改变。
    你不需要考虑数组中超出新长度后面的元素。
    解析:空间复杂度为O(1),所以只能牺牲时间了
package com.secondphase.homework.day01;

import java.util.Arrays;

/**
 * @Author jinman1012@qq.com   2020/8/11 19:22
 * @Version 1.0
 */
public class Problem3 {
    public static void main(String[] args) {
        int val = 1;
        int[] nums = new int[] {1,2,10,1,1,5,6,4,1,2,3,1,45,12,1};
        System.out.println(Arrays.toString(nums));
        int equalsCount = removeEqualsCount(val, nums);
        System.out.println("只需关注数组前" + (nums.length-equalsCount) + "位");
        System.out.println(Arrays.toString(nums));
    }

    private static int removeEqualsCount(int val, int[] nums) {
        int equalsCount = 0;
        for (int i = 0; i < nums.length-equalsCount; i++) {
            if(i == nums.length-equalsCount) {
                equalsCount++;
                break;
            }
            if(nums[i] == val){
                //防止出现连续的相同数字
                int contiSameNum = 1;
                for (int j = i; j < nums.length-equalsCount-1; j++) {
                    if (nums[i] == nums[j+1]) {
                        equalsCount++;
                        contiSameNum++;
                        continue;
                    }
                    break;
                }
                //后边的数字向前移动
                for (int j = i; j < nums.length-equalsCount-1; j++) {
                    nums[j] = nums[j+contiSameNum];
                }
                System.out.println(Arrays.toString(nums));
                equalsCount++;
            }
        }
        return equalsCount;
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值