What's wrong with my code ?
What's wrong with my code, it is not giving output properly.
Code Java:
import java.util.*;
public class Llist {
public static void main(String[] args) {
String[] things = {"Apple", "Banana", "Orange", "Mango", "Grapes"};
List<String> list1 = new LinkedList<String>();
for(String x : things)
list1.add(x);
String[] things2 = {"Briyani", "Chicken", "Meat", "Fish"};
List<String> list2 = new LinkedList<String>();
for(String y : things2)
list2.add(y);
list1.addAll(list2);
list2 = null;
printMe(list1);
removeStuff(list1, 2, 3);
reverseMe(list1);
}
private static void printMe(List<String> l) {
for(String b : l)
System.out.printf("%s ", b);
System.out.println();
}
private static void removeStuff(List<String> l, int from, int to) {
l.subList(from, to).clear();
}
private static void reverseMe(List<String> l) {
ListIterator<String> bobby = l.listIterator(l.size());
while(bobby.hasPrevious())
System.out.printf("%s ", bobby.hasPrevious());
}
}
Re: What's wrong with my code ?
What should the proper output be?
Re: What's wrong with my code ?
Ok, well i'm not sure what output you are going for but i did notice a few things:
1) Your first method printMe(list1); prints the following "Apple Banana Orange Mango Grapes Briyani Chicken Meat Fish"
2) The removeStuff method looks fine
3) There is a problem in the reverseMe method, in the last line of code
while(bobby.hasPrevious())
System.out.printf("%s ", bobby.hasPrevious());
this is an loop that goes on forever, bobby.hasPrevious() is returning true every time.
But if you tell us what output you are looking for we will be able to help you more.
Dejan