0

I've searched as far as I can look and I'm struggling to find an answer to this question.. I have an Iterable object and I want to modify each item in it (I do NOT want to add to it) with a new value. I've tried

for (T e : list) 
{
    e = a.get(index);
    System.out.println(e);
    index--;
}

to modify it but that obviously didn't work, because it is a foreach loop. Can someone show me what I'm doing wrong with this code? (The idea is that i'm trying to reverse my Iterable's item order.)

public class LinkedList<T>
{
     Iterable<T> list;
     public LinkedList() {} //create an empty list
     public LinkedList(Iterable<T> iterable)
     {
         list = iterable;
     }
     @SuppressWarnings("unchecked")
     public Iterable<T> reverse()
     {
         Integer size = size();
         List<T> a = new ArrayList<T>();
         Integer index = size - 1;
         for (T e : list) 
         {
             a.add(e);
         }
         Iterator iterator = list.iterator();
         while (iterator.hasNext()) 
         {
             T element = (T)iterator.next();
             element = a.get(index);
             System.out.println(element);
             index--;
         }
         return list;
     }
}
TurboCrackers
  • 43
  • 1
  • 10
  • 3
    As in, you want the iterable to have different elements when you're done? Not possible. Iterables don't work that way. – user2357112 Nov 11 '16 at 00:18
  • I just want to reverse the elements in the Iterable. Is that possible? @user2357112 – TurboCrackers Nov 11 '16 at 00:20
  • You could return a "reverse iterator", but none of your code modifies anything... It isn't the fact that you are using a foreach loop, either – OneCricketeer Nov 11 '16 at 00:20
  • So, what would I do to actually modify the Iterable? @cricket_007 – TurboCrackers Nov 11 '16 at 00:21
  • Check this post. http://stackoverflow.com/questions/2102499/iterating-through-a-list-in-reverse-order-in-java – OneCricketeer Nov 11 '16 at 00:21
  • Sorry, I forgot to paste the part where I returned list! I modified it! – TurboCrackers Nov 11 '16 at 00:22
  • Collections can be modified. Iterables can't. – shmosel Nov 11 '16 at 00:23
  • 1
    You can't modify the elements of an Iterable. Imagine if you could. An Iterable could be, for example, over the lines of text that you read from a network socket. How then would you modify those in order to reverse them? The answer is you can't. You can only reverse a List, or an array, or anything that has index-based get and set. – Klitos Kyriacou Nov 11 '16 at 00:26

1 Answers1

0

You need to understand this loop construct:

for (T e : list)

This assigns to the variable e, a copy of a reference to some object in list.

You tried to assign a new value to e:

e = a.get(index);

But all this achieves is overwriting your copy of the reference. The list list continues to use the reference it has always used.


Your end goal is to return an iterable that travels in the reverse direction? You have a few options here.

  • Keep the underlying list unchanged, but make a custom iterator to provide a reverse view on it. This iterator can supply an Iterable<T>, which you return.
  • Shuffle around the contents of the underlying list, such that the list itself is in reverse order (you can use Collections.reverse(list) for this). Its natural iteration order will resemble a "reverse" iteration of the original list.
  • Same as above, but instead of mutating the existing list: create a reversed copy of the list.

One thing that you're doing that will make this hard for yourself is this:

Iterable<T> list;
public LinkedList(Iterable<T> iterable) {
    list = iterable;
}

Does this need to accept something as specific as an Iterable? Could it instead accept a List (giving you the ability to lookup elements at any index)? If it has to be an Iterable, you may not have to store it as an Iterable. You could insert its contents into an ArrayList, and then your variable list could actually be a List.

Birchlabs
  • 7,437
  • 5
  • 35
  • 54
  • The point is that `e` is a different variable. The fact that the reference is copied follows automatically. – user207421 Nov 11 '16 at 03:26