Community
Participate
Working Groups
It would be nice if the order of array indices could be refactored, similar to the "Change Method Signature" refactoring. The admissible permutations might be constrained if an array is allocated with only some of the lengths specified or if references to subarrays are stored or passed (the specified and unspecified dimensions would each have to be permuted among themselves), but even the constrained reordering might be useful, and quite often I allocate all dimensions at once and only use the array as a whole, and then the refactoring would be straightforward.
@Felix: Could you please give a "before" and "after" code example ?
Here are "before" and "after" code examples, as requested. Simple case: The array is only used as a whole: Before: public class ArrayIndexReordering { public void useArray(int n) { int [] [] [] [] arr = new int [n] [4] [3] [2]; for (int i = 0;i < n;i++) arr [i] [1] [2] [0] = 1; } } After swapping the first two and the last two indices: public class ArrayIndexReordering { public void useArray(int n) { int [] [] [] [] arr = new int [4] [n] [2] [3]; for (int i = 0;i < n;i++) arr [1] [i] [0] [2] = 1; } } More complicated case: Only some of the dimensions are allocated, and a subarray is passed: Before: import java.util.Arrays; public class ArrayIndexReordering { public void useArray(int n) { int [] [] [] [] arr = new int [n] [4] [] []; for (int i = 0;i < n;i++) for (int j = 0;j < 4;j++) arr [i] [j] = new int [i + 3] [j + 2]; System.out.println(Arrays.deepToString(arr [0] [1])); } } Here, the admissible permutations would have to permute the specified and unspecified indices among themselves. For instance, swapping the first two and the last two indices would still be allowed, and the result would be: import java.util.Arrays; public class ArrayIndexReordering { public void useArray(int n) { int [] [] [] [] arr = new int [4] [n] [] []; for (int i = 0;i < n;i++) for (int j = 0;j < 4;j++) arr [j] [i] = new int [j + 2] [i + 3]; System.out.println(Arrays.deepToString(arr [1] [0])); } }
Moving to ui as this is a refactoring request