Question

How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import...

How do i rewrite this code only using the list below?

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.HashSet;

public class RotationCheck {
   /**
   * Rotates the elements in the specified list by the specified distance. After calling this
   * method, the element at index {@code i} will be the element previously at index
   * {@code (i - distance) % list.size()}, for all values of {@code i} between {@code 0} and
   * {@code list.size() - 1}, inclusive. (This method has no effect on the size of the list.)
   *
   * For example, suppose {@code list} comprises{@code ['t', 'a', 'n', 'k', 's']}.
   * After invoking {@code IntegerCollections.rotate(list, 1)} (or
   * {@code IntegerCollections.rotate(list, -4)}), {@code list} will comprise
   * {@code ['s', 't', 'a', 'n', 'k']}.
   *
   * To move more than one element forward, increase the absolute value of the rotation distance. To
   * move elements backward, use a positive shift distance.
   *
   * @param list the list to be rotated
   * @param distance the distance to rotate the list. There are no constraints on this value; it may
   * be zero, negative, or greater than {@code list.size()}.
   */
   public static void rotate(List<Integer> list, int distance) {
        Collections.rotate(list, -1*distance);
   }

   public static void main(String[] args) {
       //Create an arraylist
       List<Integer> list1=new ArrayList<Integer>();
       //Add values into list
       list1.add(1);
       list1.add(2);
       list1.add(3);
       list1.add(4);
       list1.add(5);
       //Display list
       System.out.println("Display before shift:");
       System.out.println(list1);
       //Forward shift
       System.out.println("Display after forward shift:");
       rotate(list1,-4);
       System.out.println(list1);
       //Backward shift
       System.out.println("Display after backward shift:");
       rotate(list1,1);
       System.out.println(list1);
   }

}

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;
import java.util.Set;
import java.util.HashSet;

public class RotationCheck {
   /**
   * Rotates the elements in the specified list by the specified distance. After calling this
   * method, the element at index {@code i} will be the element previously at index
   * {@code (i - distance) % list.size()}, for all values of {@code i} between {@code 0} and
   * {@code list.size() - 1}, inclusive. (This method has no effect on the size of the list.)
   *
   * For example, suppose {@code list} comprises{@code ['t', 'a', 'n', 'k', 's']}.
   * After invoking {@code IntegerCollections.rotate(list, 1)} (or
   * {@code IntegerCollections.rotate(list, -4)}), {@code list} will comprise
   * {@code ['s', 't', 'a', 'n', 'k']}.
   *
   * To move more than one element forward, increase the absolute value of the rotation distance. To
   * move elements backward, use a positive shift distance.
   *
   * @param list the list to be rotated
   * @param distance the distance to rotate the list. There are no constraints on this value; it may
   * be zero, negative, or greater than {@code list.size()}.
   */
   public static void rotate(List<Character> list, int distance) {
        Collections.rotate(list, -1*distance);
   }

   public static void main(String[] args) {
       //Create an arraylist
       List<Character> list1=new ArrayList<Character>();
       //Add values into list
       list1.add('t');
       list1.add('a');
       list1.add('n');
       list1.add('k');
       list1.add('s');
       //Display list
       System.out.println("Display before shift:");
       System.out.println(list1);
       //Forward shift
       System.out.println("Display after forward shift:");
       rotate(list1,-1);
       System.out.println(list1);
       //Backward shift
       System.out.println("Display after backward shift:");
       rotate(list1,4);
       System.out.println(list1);
   }

}

Output:

Add a comment
Know the answer?
Add Answer to:
How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import...
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
  • How can i rewrite this code?( such as changing the statement) NOTE: do not add any...

    How can i rewrite this code?( such as changing the statement) NOTE: do not add any import other than : import java.util.ArrayList; import java.util.Collection;(collection not collections) import java.util.List; import java.util.Set; import java.util.TreeSet; import java.util.HashSet; code: public static long sum(Collection values) { if(values==null || values.size()<1) return 0; int sum = 0; for (Integer i: values) sum = sum+i; return sum; }

  • How do I write this code from while loop to for loop? (this is the whole...

    How do I write this code from while loop to for loop? (this is the whole code) NOTE: do not add any import other than: import java.util.ArrayList; import java.util.Collection;(collection not collections) import java.util.List; import java.util.Set; import java.util.TreeSet; import java.util.HashSet; code: public static int frequency(Collection values, int target) { var iter = values.iterator(); int app = 0; while (iter.hasNext()) { if(iter.next() == target) { app++; } } return app; } public static boolean isSorted(List list) { var iter = list.listIterator(); int...

  • Please help with Java array list: import java.util.ArrayList; public class ListUpdate { /** * Does a...

    Please help with Java array list: import java.util.ArrayList; public class ListUpdate { /** * Does a linear search through the ArrayList list, returning the index of the first occurrence of * valToFind. * * Starting from index 0, check each element in list and return the index of the first element * that matches valToFind. * * @param valToFind The int value to search for. * @param list The ArrayList of Integers to search in. * @return The index of...

  • Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong...

    Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() {     arr = new String[10];     size = 0; } public int size() {     return size; } public void add(String value) {    ...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput;...

    package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput; /** Write a program to roll a set of dice. Generate a random number between 1 and 6 for each dice to be rolled, and save the values in an ArrayList. Display the total of all the dice rolled. In some games, rolling the same number on all dice has a special meaning. In your program, check if all dice have the same value,...

  • PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class...

    PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...

  • import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[]...

    import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[] arr = new int[100000];    public static void main(String[] args) {        for(int i=0; i<100000; i++)            arr[i] = i;               //TODO comment out this line        LinkedList<Integer> list = new LinkedList<Integer>();               //TODO uncomment this line        //List<Integer> list = new ArrayList<Integer>();               //TODO change the rest of the...

  • How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.Loc...

    How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; /** * Utility class that deals with all the other classes. * * @author EECS2030 Summer 2019 * */ public final class Registrar {    public static final String COURSES_FILE = "Courses.csv";    public static final String STUDENTS_FILE = "Students.csv";    public static final String PATH = System.getProperty("java.class.path");    /**    * Hash table to store the list of students using their...

  • Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import...

    Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...

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