Menu
Question Index
...


Write a program to move a String in the list by given number of positions.

Input (Strings List, String to be moved, how many positions to move) Output (Moved List)
List = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"]
String to be moved = "Apr"
Positions = 2
["Jan", "Feb", "Mar", "May", "Jun", "Apr", "Jul"]
List = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"]
String to be moved = "Apr"
Positions = -1
["Jan", "Feb", "Apr", "Mar", "May", "Jun", "Jul"]
List = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"]
String to be moved = "Jan"
Positions = 10
["Feb", "Mar", "Apr", "May", "Jun", "Jul", "Jan"]
List = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"]
String to be moved = "Mar"
Positions = -4
["Mar", "Jan", "Feb", "Apr", "May", "Jun", "Jul"]

class MoveAStringInTheList

{    public static void main(String s[])
    {
        List months = new ArrayList();
        months.add("Jan");
        months.add("Feb");
        months.add("Mar");
        months.add("Apr");
        months.add("May");
        months.add("Jun");
        months.add("Jul");
        
        System.out.println("Months list before moving : " + months);
        
        moveString(months, "Apr", 2);
        
        System.out.println("Months list after moving : " + months);

    }


public static void moveString(List<String> inputList, String stringToBeMoved, int positions)
{
//Write code here to move the string by the given number of positions. The positions could be both negative and positive.
}
}

Doubts

Problems

Topic: Java ArrayList

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App