Question

In: Computer Science

Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an...

Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an ArrayList. Then search for the first instance of the number 3, print the position, and then remove it from the list.

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Random;

public class RandomArrayList {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Random rand = new Random();
        for(int i = 0;i<100;i++){
            list.add(rand.nextInt(10));
        }

        System.out.println("ArrayList: "+list);
        int index = list.indexOf(3);
        if(index!=-1) {
            list.remove(index);
            System.out.println("Position of the value 3 is "+index);
        }
        else{
            System.out.println("3 not found");
        }
        System.out.println("ArrayList after removal of 3: "+list);
    }
}

ArrayList: [9, 1, 7, 8, 7, 5, 4, 0, 4, 2, 4, 8, 0, 2, 7, 9, 9, 0, 5, 4, 4, 5, 4, 2, 6, 6, 1, 4, 1, 5, 3, 2, 9, 4, 3, 4, 4, 4, 6, 2, 7, 6, 8, 7, 5, 2, 4, 3, 0, 9, 5, 5, 0, 9, 3, 0, 2, 7, 6, 3, 8, 9, 2, 1, 6, 6, 2, 7, 3, 0, 0, 7, 3, 5, 3, 3, 6, 2, 5, 2, 3, 9, 3, 1, 6, 1, 6, 8, 2, 2, 3, 6, 6, 7, 9, 7, 5, 1, 0, 7]
Position of the value 3 is 30
ArrayList after removal of 3: [9, 1, 7, 8, 7, 5, 4, 0, 4, 2, 4, 8, 0, 2, 7, 9, 9, 0, 5, 4, 4, 5, 4, 2, 6, 6, 1, 4, 1, 5, 2, 9, 4, 3, 4, 4, 4, 6, 2, 7, 6, 8, 7, 5, 2, 4, 3, 0, 9, 5, 5, 0, 9, 3, 0, 2, 7, 6, 3, 8, 9, 2, 1, 6, 6, 2, 7, 3, 0, 0, 7, 3, 5, 3, 3, 6, 2, 5, 2, 3, 9, 3, 1, 6, 1, 6, 8, 2, 2, 3, 6, 6, 7, 9, 7, 5, 1, 0, 7]

Related Solutions

Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Write a java program that inserts 25 random integers from 0 to 100 in order into...
Write a java program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
The language is MATLAB Write a function that will generate three random integers, each in the...
The language is MATLAB Write a function that will generate three random integers, each in the inclusive range from 10 to 80. It will then return a string consisting of the three integers joined together, and also a character vector consisting of the three integers joined together. For example, if the random integers are 11, 29, and 76, the string that is returned will be "112976" and the character vector that is returned will be '112976'. I'm really confused on...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the...
Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the user to guess four numbers.  Compare each of the user’s guesses to the four random numbers and display a message that includes the user’s guess, the randomly determined four-digit number, and the amount of points the user has won as follows: No matches 0 points Any one digit matching 5 points Any two digits matching 100 points Any three digits matching 2,000 points All four...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT