I'm having trouble with creating a method for a homework problem to reverse an ArrayList
in place so that 1 2 3 4 5 for example would become 5 4 3 2 1. I know about Collections.reverse
, but I can't use it and I can't use a regular for loop, it has to be a "for each" loop so "for (String book: books) { ... }
". I know that using a regular for loop I would be able to use two iterating variables i
and j
so that I could swap values of the first and last index to reverse it, but in a for each loop you can't do that. Any help would be appreciated. Thanks.
Asked
Active
Viewed 2,405 times
-4

John Kerry
- 45
- 6
-
6Given the restrictions you stated I have to ask, Is this homework? If it is, you ought to mention that. Homework is _not_ automatically disqualified from SO, and if you say it is, people tend to give more helpful hints to teach you rather than drop a solution with no explanation. – Stephen P Sep 25 '18 at 00:35
-
4Post your code (even psoudo code) for what you did so far. recommendation #1: try solving it with a regular for loop and then try to see how to convert it to for-each (not the same, I know). Recommendation #2: https://stackoverflow.com/questions/1098117/can-one-do-a-for-each-loop-in-java-in-reverse-order – adhg Sep 25 '18 at 00:37
-
1Hint: you can still declare `int` counters outside the loop and increment them as part of the loop body. – Janus Varmarken Sep 25 '18 at 00:37
-
Hint: ArrayList.size() gives you number of objects inside arraylist – bakero98 Sep 25 '18 at 00:40
-
I am guessing if this is a homework, it would be meant to teach you about Iterable. So read through the Iterable interface (https://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html) and learn how you can implement it. – nupadhyaya Sep 25 '18 at 00:42
-
If you don't want to any indices at any cost, you could create a new linked list, and use "addLast" and return new ArrayList<>( linkedlist ). Having said that, the efficient way to do it is using swap as you said, but you need indices. – SomeDude Sep 25 '18 at 00:45
-
1A couple of answers since I posted my first comment bring up this question: do you have to reverse the list _in-place?_ That is, given `ArrayList al` are you required to reverse the order of `al` itself, or are you allowed to create & return a _new_ array that is in the reverse order of `al`? _**Or**_ do you merely have to _print_ the reverse of the original array (as bakero98 does)? A clear statement of the problem and goal is _essential_ to Coding the Right Thing. – Stephen P Sep 25 '18 at 00:52
-
I have to reverse the list in place, I'll add that to the op – John Kerry Sep 25 '18 at 00:54
-
@JohnKerry I updated my answer so you should give it a look – bakero98 Sep 25 '18 at 01:19
2 Answers
1
This is example using for each
you create variable containing number of strings inside arrayList and then use regular for each
loop to go through whole arrayList
but you print on position i
and decrement i every time
int i = books.size() - 1;
for(String b: books){
newList.add(books[i]);
System.out.println(books[i]);
i--;
}
This is example for switching inside one arrayList.
int i = books.size() - 1;
String helpString;
for(int n = 0; n <= books/2 ; n++){
helpString = books[n];
books[n] = books[i];
books[i] = helpString;
i--;
}

bakero98
- 805
- 7
- 18
1
You can do this by using forEach syntax.
List<Integer> intValues = new ArrayList<Integer>();
intValues.add(1);
intValues.add(2);
intValues.add(3);
intValues.add(4);
intValues.add(5);
IntStream.range(0, intValues.size()).forEach(v -> System.out.print(intValues.get((intValues.size()-1)-v)));

Deepak Singh
- 411
- 1
- 5
- 13