1

So I was trying to write a method which returns the reverse of given ArrayList but it gives me Index error and I don't know how to solve it. I appreciate your help. This is my code:

import java.util.ArrayList;

public class question2 {

public static ArrayList<Double> reverse(ArrayList<Double> arraylist) {
    ArrayList<Double> result = new ArrayList<Double>();
    for(int i=0; i<arraylist.size(); i++) {
        result.set((arraylist.size()) - 1 - i, arraylist.get(i));
    }
    return result;
}

public static void main(String[] args) {
    
    ArrayList<Double> doubles = new ArrayList<Double>();
    doubles.add(1.5);
    doubles.add(2.3);
    doubles.add(7.7);
    doubles.add(8.6);
    System.out.println(reverse(doubles));
    
}

}

And this is the error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 0
1
  • 1
    Read the documentation of the set method. Commented Oct 7, 2020 at 17:57

4 Answers 4

1
public static ArrayList<Double> reverse(ArrayList<Double> arraylist) {
       ArrayList<Double> result = new ArrayList<Double>(arraylist.size());
        for(int i=arraylist.size()-1; i>=0; i—-) {
            result.add(arraylist.get(i));
        }
        return result;
     }

Hope this will work!

Sign up to request clarification or add additional context in comments.

9 Comments

I changed the first part with ArrayList<Double> result = ArrayList<Double>(arraylist.size()) and the arraylist.set with arraylist.add but it didn't work.
Please upvote and mark it as true answer. Thanks in advance
It still doesn't work and throws same exception (I just copy pasted)
Can you use Collections.reverse(arraylis);
I can't, I have to write it myself because it's for school.
|
0

Can you try my code , it will work for you

 public static List<Double> reverse(ArrayList<Double> arraylist) {
        List<Double> result = Arrays.asList(new Double[arraylist.size()]);
        for(int i=0; i<arraylist.size(); i++) {
            result.set((arraylist.size()) - 1 - i, arraylist.get(i));
        }
        return result;
    }

1 Comment

Hmm it gives some kind of error. But I guess that's because I didn't import needed libraries. Thanks for your answer :)
0
public static String reverse(ArrayList<Double> arraylist) {
ArrayList<Double> result = new ArrayList<Double>();
for(int i=arraylist.size() -1; i>=0; i--) {
    result.add(arraylist.get(i));
}
return result.toString();

}

the return type of reverse() method should be char[] or String as you are printing on the console from the main method

Comments

0

Here is something you may want to try. It does not require a separate List and only loops half as many times as copying the entire list to another one.

It works by swapping the values at the beginning and the end as the loop index moves closer toward the middle.

  • set size = list.size();
  • iterate the loop from 0 to i < size/2
  • save the value at i to a variable.
  • set the position at i to the value at size - i - 1
  • set the position at size - i - 1 to the saved value.

This will proceed until the loop has reached the middle. Note that for even sized lists, the inner most values are adjacent and swapped. For odd sized lists, the inner most item is ignored since it is correctly positioned.

2 Comments

Thanks for your answer. But I can't use pre-made methods since this is some kind of homework. I have to write it myself. But thanks anyway :)
Yes, this also works and in a different way. Thanks for your answer. I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.