package com.codegym.task.task07.task0716;
import java.util.ArrayList;
/*
R or L
*/
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add("rose"); // 0
list.add("love"); // 1
list.add("lyre"); // 2
list = fix(list);
for (String s : list) {
System.out.println(s);
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
int j=0;
ArrayList<String> list1 = new ArrayList<String>();
list1.addAll(list);
for(int i=0;i<3;i=j)
{
if(list1.get(i).contains("r")&&!(list1.get(i).contains("l"))){
list1.remove(i);
j=0;
}
if(!(list1.get(i).contains("r"))&&list1.get(i).contains("l")){
list1.add(list.get(i));
j=j+1;
}
if(list1.get(i).contains("r")&&list1.get(i).contains("l")){
continue;
}
if(!(list1.get(i).contains("r"))&&!(list1.get(i).contains("l"))){
continue;
}
}
return list1;
}
}
//1.The program must not read data from the keyboard.
//2.The fix method should remove all words containing the letter "r" from the list. There is an exception: words containing both "r" and "l" should be left alone.
//3.The fix method must duplicate words containing the letter "l" (add another instance of this word to the list). There is an exception: words containing both "l" and "r" should not be duplicated.
//4.The fix method should not do anything with words that contain both "l" and "r".
//5.The fix method should not do anything with words that don't contain either "l" or "r".
unable to get output and can we do it without using 2nd arraylist
Resolved
Comments (12)
- Popular
- New
- Old
You must be signed in to leave a comment
Andrew
20 March 2019, 21:25
There is an infinite loop when j = 1. The last two if statements do not modify j so it just repeats infinitely.
Did you know that you can modify the for loop like this:
By leaving off the i = j, you can remove int j completely and you can assign the value of i within each if statement.
For example:
Change the order of your if statements so the two conditions that don't modify the list are first.
If you make the changes to the for loop suggested above and remove int j, you will need to think about how to influence i (or not) in each if statement.
One more suggestion: you hard code 3 for the length of the list into your for loop. It's best practice to do something like list.size() instead of hard coded numbers, in case the length of the list is different than 3.
As to your other question:
Since the variable "list" is just a reference to your ArrayList, you don't need list1. You can just do:
instead of:
And fix() doesn't need to return anything, it can be void. Replace all "list1" references with "list" since that's what you call it in the fix method parameter.
Another way to think of it is "list" is the address to a house.
The fix() method says "Do something to the house at the address in the parameter."
As long as you don't change the address to the house (its variable name), or assign it to a different house, then that address will always get you to that house, before and after you "do something" to that house. +2
Mark Fernandez
21 March 2019, 07:50
Thanks Andrew for the response and pointing out the errors in it i will surely look in to it and make the necessary changes . Since new to programming didn't know much about arraylist and other stuff but now im getting hang of it.
0
Guadalupe Gagnon
21 March 2019, 13:23
Just a quick point: The fix method is part of the starting code of this task. I wouldn't change the return type because this MAY automatically fail verification. It also may not matter, but in most cases the verification system tests the code a certain way and that way is most likely mirrored in the starting code.
0
Andrew
21 March 2019, 15:56
Thanks Guadalupe, I missed that.
0
Guadalupe Gagnon
21 March 2019, 17:13
You know how I know to look for that.... because I have made the same mistake. ;-)
0
Mark Fernandez
21 March 2019, 21:00
public static ArrayList<String> fix(ArrayList<String> list) {
for(int i=0;i<list.size();i++)
{
if(list.get(i).contains("r")&&!(list.get(i).contains("l"))){
list.remove(i);
}
else if(!(list.get(i).contains("r"))&&list.get(i).contains("l")){
list.add(i+1,list.get(i));
i++;
}
else if(list.get(i).contains("r")&&list.get(i).contains("l")){
continue;
}
else if(!(list.get(i).contains("r"))&&!(list.get(i).contains("l"))){
continue;
}
}
return list;
}
I have made some changes here and now i am getting o/p
love
lyre
which is wrong, but if i rearrange the seq of the inputs i.e. rose lyre love then i am getting correct o/p can you help why it is happening or how to fix above code.
0
Andrew
22 March 2019, 14:55
Since you have i++ in your for loop, each time you remove a list item, it will move on to the next index but when you remove an item from a list, it moves the remaining items to the left to fill any holes. For example if i = 0 and you remove that item, the item that was previously at index 1 moves to index 0, but then the for loop moves on to index 1 which is the item that was formerly at index 2.
+2
Mark Fernandez
22 March 2019, 18:08
can you help me to fix it
0
Mark Fernandez
22 March 2019, 18:13
If i try to remove i++ from there it stops working and i understand about the removing part which you have explained.
0
Andrew
22 March 2019, 18:50
When you remove i++ from the for loop, you need to tell each if statement what to do with i so the loop keeps running correctly.
For example, with the if statements that don't change the list, you just want to go on to the next index. Instead of "continue" you should increase i.
You will have to change the other if statements too. When you remove an item from the index, you don't want to move i for the next iteration for the reasons in my last post. When you add an item, you want to advance the index by 2 instead of 1.
+1
Andrew
22 March 2019, 18:59
I can't remember if you've gotten to while loops yet but if it helps, here's another way to think about it:
is the same as:
0
Kent Hervey Software Engineer/Consult Expert
16 November 2019, 00:18
I think I might switch to a while. I used a for and verification passed, but the code style check is complaining: Control variable i is modified
0