I don't know how to exclude the elements of the array.

Asked 2 years ago, Updated 2 years ago, 30 views

以下 In the program below, if the element of the list type variable listnumber contains zero, I want to exclude zero, so I use the remove method, but I get the following error.What should I do?

Main2.java:40:Error: Cannot find symbol
            listnumber.remove(0);
                      ^
  symbols:method remove(int)
  location:change in type int[]

数字 Enter the numbers and put them in the array, but if the same numbers are in multiple arrays, I would like to remove the numbers afterwards.What should I do?
I think if(list number[i]==listnumber[i]){...}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.List;

public class Main2 {
    /**
     * Iterate through each line of input.
     */

    public static void main(String[]args) rows IOException {
        // InputStreamReader = new InputStreamReader (System.in, StandardCharsets.UTF_8);
        // BufferedReader in = new BufferedReader (reader);

        Scanner sc = new Scanner (System.in);

        int listnumber[] = new int[10];

        for(inti=0;i<10;i++){
            int number = sc.nextInt();
            listnumber[i] = number;
        }

        System.out.println("nyuryoku End";

        for(inti=0;i<10;i++){
            If the value of //list is 0, exclude it. 
            if(listnumber[i]==0){
                listnumber.remove(0);
            }

            System.out.println(listnumber[i]);
        }
    }
}

Run Environment

openjdk version "1.8.0_242"
OpenJDK Runtime Environment (AdoptOpenJDK) (build 1.8.0_242-b08)
OpenJDK64-Bit Server VM (AdoptOpenJDK) (build 25.242-b08, mixed mode)

java

2022-09-30 16:05

1 Answers

(1)
The question says "list number" but the question program says

 int listnumber[] = new int[10];

So listnumber is an array of integers.I think the contradiction (misunderstanding) that is happening here is complicating the problem.

There are two ways to fix it.
[Method 1] Once you put data into an array, convert it to a list, remove elements with specific values by the remove method, and then convert the list to an array.

[Method 2] Declare a list number of type list, such as ArrayList listnumber = new ArrayList(); and add a value with listnumber.add in the first for statement.The remove method then removes elements with specific values.

===
(2) Use the dictionary (java.util.Dictionary).
I add it to the dictionary when I put it in the array, and when I put it in the array, I check if it is in the dictionary, and if it is already in the array, I cannot put it in the array.This prevents multiple identical numbers from entering the array.


2022-09-30 16:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.