Question

In: Computer Science

please code in java: There's an arraylist of "words" with 250,000 words. Pick 50,000 randomly chosen...

please code in java:

There's an arraylist of "words" with 250,000 words. Pick 50,000 randomly chosen words from "words" and add them to a "search" array (not Arraylist) of strings.

Solutions

Expert Solution

Thanks for the question, selecting 50,000 words from 250,000 words is like selecting 1 word for every 5 words.

We can divide the 250,000 words into 50,000 groups where each group contains 5 words, and then selecting one word from each group.

First group starts with index 0 and ends with index 4
Second group starts with index 5 and ends with index 9
...
...

Likewise and so forth.

Below is the code that you will be needing. Let me know for any questions or help.


===========================================================================


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

public class RandomSearchWords {

    public static void main(String[] args) {

        ArrayList<String> words = new ArrayList<>(250000);
        Random random = new Random();

        String search[] = new String[50000];
        int index = 0;
        int randomIndex = 0;
        for (int i = 0; i < search.length; i++) {
            randomIndex = i * 5 + random.nextInt(5);
            search[i] = words.get(randomIndex);
        }

    }
}

==========================================================================

thanks !


Related Solutions

Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word: banana dresser grammar potato revive uneven assess Write a program that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters as lowercase letters.
in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
In java, please Create an ArrayListReview class with one data field of ArrayList with the generic...
In java, please Create an ArrayListReview class with one data field of ArrayList with the generic type passed to the class. (1 point) Create a constructor that populate an array list filled with the generic type through inserting new elements into the specified location index-i in the list. (1 point) Implement mergeSort using a list and recursion. (2 points) Write the main method to test your program and use System.nanoTime() to find out the speed of each step of your...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
In java, (include javadoc comments for each method, please use ArrayList, not Array, and ensure that...
In java, (include javadoc comments for each method, please use ArrayList, not Array, and ensure that the program compiles). design a class named Contact that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then, write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT