To enter and delete arbitrary values in a Java array...

Asked 2 years ago, Updated 2 years ago, 106 views

public static int ListInsertDeletetCount(int []array){
 int count = 0;
 Random rnd = new Random();
     for(int i=0; i<100; i++){
        int dst = rnd.nextInt(list.length-1);
        for(;;) {
           //Insert a dst number in the dst location
         count++;
       }
        int dat = rnd.nextInt(list.length-1);
        for(;;){
           Delete from //dst location
        count++;
       }
   count = count / 200;
   return count;
}

The code is the same as above You have to enter random values in random arrangement and repeat them Isn't the insert list[dst] = dst;? I keep getting errors I don't know what delete is. I'm asking because it can't be solved by removing.

java data-structure

2022-09-22 08:35

1 Answers

You said there was an error first, but what is the error message?

The code does not have a variable declaration called list when looking at only the given code.

In other words, you are handling variables that do not even exist.

Also, aren't you handling elements of the array when you're receiving a reference variable (array)?

Array is a kind of fixed data type. Here, fixed data type means that once assigned, it cannot be changed.

Declaring an array of 10 ints and erasing one does not mean that there are 9 data types.

Then, can't I delete the element with the array? No, in fact, we're not deleting it, we're removing the elements we want to delete, and we're copying the rest to a new array.

The Java side data structure implemented in that way is ArrayList.

In other words, the data structure that the questioner wants is ArrayList. Changing to ArrayList instead of array will solve it easily, and if you want to know how to handle it as an array, you can look at the ArrayList source code.


2022-09-22 08:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.