题解 | #序列中删除指定数字#

序列中删除指定数字

https://www.nowcoder.com/practice/7bbcdd2177a445a9b66da79512b32dd7

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // int[] 的长度是不可变的,要能够删除的话得用集合的方式
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            ArrayList<Integer> array = new ArrayList<Integer>();
            for (int i = 0; i < a; i++) {
                array.add(in.nextInt());
            }
            int b = in.nextInt();
            for (int y = 0; y < array.size(); y++) {
                if (array.get(y) == b) {
                    array.remove(y);
                    y = y - 1;
                }
            }
            for (int x = 0; x < array.size(); x++) {
                System.out.print(array.get(x) + " ");
            }

        }
    }
}

全部评论

相关推荐

10-01 09:50
门头沟学院 Java
肖先生~:这个人真的很好,点赞
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务