0

I'm working with Processing and IGeo library and I have an ArrayList of IVec[] arrays:

ArrayList<IVec []> v = new ArrayList<IVec[]>(); 

For every I of the ArrayList I have a collection of IVec [] arrays that represent the coordinates of the control points of a curve. I need to reverse the order of the IVec [] control points keeping the same order of the ArrayList (I'm trying to invert curve seam reversing control points order and keeping the original order of the curves) but I can't understand how to do this. Can anyone help me?

5 Answers 5

1

I won't provide you a full solution, but will guide you through it:

  • Iterate on the array list v
  • for each item in it (IVec[]),
Sign up to request clarification or add additional context in comments.

3 Comments

When I try to use Arrays.AsList I get the error The function AsList(IVec[]) does not exist, I supposed related to IVec[] properties but not sure. I'm trying to iterate over the ArrayList to change the index manually with the for loop.
@liuz_m it's asList, with small a.
Thank you for your help! I have corrected my mistake but now I get cannot convert from List<IVec>to List
1

You can use Collections.reverse

You can also use a stack data structure. You can iterate over collection you wish to reverse by pushing the elements into the stack. Then, when you actually want to use the elements, you pop each element from the stack, which will allow you to iterate over the collection in reverse order.

Comments

0

This solution is working:

for (int i=0; i<v.size (); i++) {  
  IVec [] vert=v.get(i); 
  for (int j=0; j<vert.length/2; j++) {
    IVec temp = vert[j];
    vert[j]=vert[vert.length -1 - j];
    vert[vert.length - 1 - j] = temp;
  }
}

Comments

0

Try this;

  • Create a Helper method/function that takes and returns array.
  • Inside the Helper method, use Collections.reverse
  • return the reversed array.
  • call this helper method inside a loop as below:
for(int i = 0; i < OutArray.length; I++)
{ // Here get the inner Array and pass it to Helper method. 
// Add the return array to newArray List 
}
return newArrayList.

1 Comment

This post should elaborate on the solution rather than just post code and expect users to perform blind copy-pastes with getting additional understanding.
0

This should work (not the most efficient way, but easily understood):

public static <T> void reverseElements(ArrayList<T[]> list) {   

    ArrayList<T> tempList = new ArrayList<T>();
    for(T[] arr : list) {

        tempList.clear();
        for(T t : arr)
            tempList.add(t);

        Collections.reverse(tempList);
        tempList.toArray(arr);
        arr = tempList.toArray(arr);
    }       
}

Comments

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.