Question

How do you do reverse arrayList? using recursion? displaying it for example Normal array list 1...

How do you do reverse arrayList? using recursion? displaying it for example

Normal array list 1 2 3 4 5 6 7

reverse array list 7 6 5 4 3 2 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

class ReverseArrayList

{

public static void main(String args[])

{

ArrayList<String> a1 = new ArrayList<String>();

a1.add("1");

a1.add("2");

a1.add("3");

a1.add("4");

a1.add("5");

a1.add("6");

a1.add("7");

System.out.println("\nNormal ArrayList : " + a1);

List<String> output = reverseListRecursively(a1);

System.out.println("\nReversed ArrayList : " + output);

System.out.println();

}

  

private static List<String> reverseListRecursively(List<String> list)

{

if (list.size() <= 1)

{

return list;

}

List<String> reversed = new ArrayList<>();

reversed.add(list.get(list.size() - 1));

reversed.addAll(reverseListRecursively(list.subList(0, list.size() - 1)));

return reversed;

}

}

s c:\Users\u se r \Desktop> ]avac Re verseArrayList,java PS C:\Users\user Desktop> java ReverseArrayList Normal ArrayList [i,

Add a comment
Know the answer?
Add Answer to:
How do you do reverse arrayList? using recursion? displaying it for example Normal array list 1...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array...

    USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array of integers (and the size of the array in the C/C++ version). The function will reverse the array of integers and return the reversed array. Print the array that is returned from the function. How you "return" the array is up to you, but do not overwrite the original array. Note: in Python, the term list is more appropriate, see https://docs.python.org/3.5/tutorial/datastructures.html a) Example input:...

  • Add the following methods to the ArrayList class that we wrote during lecture. You may call...

    Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • question 27’s end is in the next page 24 Suppose you have created a list using the statement ArrayList String> NYIT CS Courses new ArrayList(31 What would java do when you try to insert more...

    question 27’s end is in the next page 24 Suppose you have created a list using the statement ArrayList String> NYIT CS Courses new ArrayList(31 What would java do when you try to insert more than 31 courses? ANS: LinelI) check one with x: continue process as usual I Ijava automatically expand array space you manually expand more array spaces 25 Suppose NYIT CS COurscs is an object of the class Array List Suring Write Java statements that will display...

  • Efficiency of Array vs. ArrayList-- Use JAVA to implement the operations Using the list operations fill,...

    Efficiency of Array vs. ArrayList-- Use JAVA to implement the operations Using the list operations fill, increment, and search, investigate whether arrays or ArrayLists are faster, or whether they are about the same for int and float values. This will also test times to generate both int and float random numbers as well as the time cost of automatic expansion of an ArrayList. Remember: int and float are used in simple arrays, Integer and Float are used in the ArrayList....

  • 1.arrayList[9] accesses the 10th object of an array of length 10 true false 2. Arrays are...

    1.arrayList[9] accesses the 10th object of an array of length 10 true false 2. Arrays are the only data structure that can be accessed with [ ]; for example: orderedData[0] true false 3. In a maximum of three steps: how do you swap the contents between two variables of the same type? Pick the absolute best answer A temp = x; x = y; y = temp B x = x + y; y = x - y; x =...

  • Assignment 6: Recursion Learning Outcomes • Learn how to craft solutions using recursion instead of loops....

    Assignment 6: Recursion Learning Outcomes • Learn how to craft solutions using recursion instead of loops. Instructions This assignment will be different than previous assignments (and most assignments which come after it). In this assignment, you will be crafting four solutions to four different problems. This assignment will also have special requirements regarding how you may code. You are not allowed to use assignment statements. This includes using preincement, postincrement, predecrement, and postdecrement. You are allowed to use assignment to...

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with  alternating signs.      // For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign.  ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT