Menu
Topics Index
...
`

Move A String In The List


If you need explanation Read this topic

If you need Answer Take test on this topic

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"]


import java.util.*;
class MoveAStringInTheList
{
    public static void main(String s[])
    {
        List<String> months = new ArrayList<String>();
        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)
    {
    }

}
Topic: Java ArrayList

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

moveString(months,"apr",-1);
move String (months, "Jan", 10);
moveString (months, "mar", -4);

Posted by Fuzail Ahmed    2014-12-10 06:30:13


steps to follow..1.for loop to find actual position of the string.2,if condition to check positive or negative value .if positive use 1 for loop to shift that string right side.3, if value negative use for loop to shift that string to left.here pass by reference method used.so if we made any changes in this function. thn original list also automatically get updated

Posted by Maheshwari Natarajan    2014-12-10 19:19:12


Here we the List which is Help full reduce the code..

here 1st get the index of the String which we want to move...by using the indexOf()...store that value in variable...and also store the size of the list
remove the string from the list which we want to move...Removing Bcz all elements will get shifted..
now we want add the new element which we want move..Bcz we are already removed..so we need get the new position to add to find new position index+numofpositions to move...here check if it gets more than size than make it to size this can done by ternary operator..newPosition=(index+positions>size)?size:(index+positions)
now check the new position it may goes below 0 Bcz we are passing negative values also if it is less than 0 so assign it to 0

Posted by Uday Kumar    2014-12-11 13:07:23


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Second Correct Comment'. The 'lucky liker' is Vikas Tiyagi. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-14 04:30:33


© meritcampus 2019

All Rights Reserved.

Open In App