Question

Build two simple arrays (Integer and String) of 6 in length. Convert the two arrays into...

Build two simple arrays (Integer and String) of 6 in length. Convert the two arrays into a List type using an ArrayList type. A static method Array2Collection generic method is provided for that. From the List, display the contents of the two arrays in the forward or backward order: (without using an index for loop)

//Code for converting Array to a Collection type

static <T> void Array2Collection(T[] a, Collection<T> c) {

    for (T x : a) {

        c.add(x); // Correct

    }

}

Output:

lint: [0, 1, 3, 5, 7, 9]

list: [Karl, Mike, Arthur, Ray, Sydney, Kayla]

lint: forward order

0 1 3 5 7 9

lint: reverse order

9 7 5 3 1 0

list: forward order

Karl Mike Arthur Ray Sydney Kayla

list: reverse order

Kayla Sydney Ray Arthur Mike Karl   

The original lists (lint, list)

lint: [0, 1, 3, 5, 7, 9]

list: [Karl, Mike, Arthur, Ray, Sydney, Kayla]

Homework Answers

Answer #1

To solve this question first create two arrays one for storing the integer and other to store the strings. Here two arrays names and integers are created to store the respective values. After that create two empty ArrayList namely n1 and n2 and call the function Array2Collection() as provided in the question to convert the array and add the values to it.

After that In order to print without index-based loop use for each loop to print the content of the ArrayList. The Last step is to reverse the order, So to do that just use Collections.reverse() function which will reverse the ArrayList.

The necessary comments that will help you understand the solution.

import java.util.*;
class Test{

    // Conversion method
    static <T> void Array2Collection(T[] a, Collection<T> c) {

        for (T x : a) {
    
            c.add(x); // Correct
    
        }

    }    
    public static void main(String[] args) {
        // Two Arrays
        String[] names = {"Karl", "Mike", "Arthur", "Ray", "Sydney", "Kayla"};
        Integer[] integers = {0,1,3,5,7,9};
        
        ArrayList<Integer> n1 = new ArrayList<Integer>();
        ArrayList<String> n2 = new ArrayList<String>();
        
        Array2Collection(integers, n1);
        Array2Collection(names, n2);

        ArrayList<Integer> s1 = new ArrayList<Integer>(n1);
        ArrayList<String> s2 = new ArrayList<String>(n2);


        System.out.println("Lint: " + n1);
        System.out.println("list: " + n2);

        // Lint Forward and Backward
        System.out.println("lint: forward order ");       
        for(Integer i: n1){
            System.out.print(i+ " ");
        }
        System.out.println();
        Collections.reverse(n1);
        System.out.println("lint: reverse order ");
        for(Integer i: n1){
            System.out.print(i+ " ");
        }
        System.out.println();
        
        // List Forward and BackWard
        System.out.println("list: forward order" );
        for(String i: n2){
            System.out.print(i+ " ");
        }
        System.out.println();
        Collections.reverse(n2);
        System.out.println("list: reverse order" );
        for(String i: n2){
            System.out.print(i+ " ");
        }
        System.out.println();
        
        // Printing orginal list
        System.out.println("The orginal lists(lint,list)");
        System.out.println("lint: " + s1);
        System.out.println("list: " + s2);

    }   

}

I hope you have understood the solution. If you like the solution kindly upvote it.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] W X D T P N R Q K M E If the array was being sorted using the SHELL sort and the halving method, and sorting into ASCENDING order as demonstrated in the course content, list the letters in the resulting array, in order AFTER the FIRST pass. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
I NEED THIS ANSWER FOR MY DATA STRUTURE CLASS: Consider the following two methods: public static...
I NEED THIS ANSWER FOR MY DATA STRUTURE CLASS: Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the...
Q. Write a method add(PolyTerm): Two PolyTerm objects can be added if and only if they...
Q. Write a method add(PolyTerm): Two PolyTerm objects can be added if and only if they have the same exponent. If the calling object can be added to the parameter object, return their sum, otherwise (if they can't be added), return null. For example 2x^3 + -7.3x^3 = -5.3x^3 while 2x^3 + 8x^2 should return null. (It's on JAVA) The JUnit Test for this question is: public class PolyTermTest {    public static int score = 0;    public static...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the n movies in her collection. She's getting ready for a film festival and wants to choose some subsequence of movies from her collection to bring such that the following conditions are satisfied: The collective sum of their ratings is maximal. She must go through her list in order and cannot skip more than one movie in a row. In other words, she cannot skip...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT