patternjavaMinor
Array slice type in Java
Viewed 0 times
arraytypejavaslice
Problem
Edit See the next iteration at Array slice type in Java - follow-up.
I have this "slice" type for managing array subranges. It is kind of the same thing as Pythons slice notation, yet I did not add negative indexing (since Java's
Slice.java:
```
package net.coderodde.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* This utility class implements cyclic array slices. If you move the
* slice to the right long enough, the "head" of the slice will wrap around and
* emerge at the beginning of the array being sliced. Same applies to movement
* to the left.
*
* @author Rodion "rodde" Efremov
* @param the actual array component type.
*/
public class Slice implements Iterable {
/**
* The actual array being sliced.
*/
private final E[] array;
/**
* The starting index of this slice within array.
*/
private int fromIndex;
/**
* The size of this slice.
*/
private int size;
/**
* Constructs a slice representing the entire array.
*
* @param array the array being sliced.
*/
public Slice(final E[] array) {
this(array, 0, array.length);
}
/**
* Constructs a slice representing everything starting at index
* fromIndex.
*
* @param array the array being sliced.
* @param fromIndex the starting index.
*/
public Slice(final E[] array, final int fromIndex) {
this(array, fromIndex, array.length);
}
/**
* Constructs a new slice for array starting at
* fromIndex and ending at toIndex - 1.
*
* @param array the array being sliced.
* @param fromIndex the starting (inclusive) index.
* @param toIndex the ending (exclusive) index.
*/
public Slice(final E[] array,
final int fromIndex,
final int toIndex) {
checkArray(array);
checkIndex
I have this "slice" type for managing array subranges. It is kind of the same thing as Pythons slice notation, yet I did not add negative indexing (since Java's
Lists don't do it). So, what you think?Slice.java:
```
package net.coderodde.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* This utility class implements cyclic array slices. If you move the
* slice to the right long enough, the "head" of the slice will wrap around and
* emerge at the beginning of the array being sliced. Same applies to movement
* to the left.
*
* @author Rodion "rodde" Efremov
* @param the actual array component type.
*/
public class Slice implements Iterable {
/**
* The actual array being sliced.
*/
private final E[] array;
/**
* The starting index of this slice within array.
*/
private int fromIndex;
/**
* The size of this slice.
*/
private int size;
/**
* Constructs a slice representing the entire array.
*
* @param array the array being sliced.
*/
public Slice(final E[] array) {
this(array, 0, array.length);
}
/**
* Constructs a slice representing everything starting at index
* fromIndex.
*
* @param array the array being sliced.
* @param fromIndex the starting index.
*/
public Slice(final E[] array, final int fromIndex) {
this(array, fromIndex, array.length);
}
/**
* Constructs a new slice for array starting at
* fromIndex and ending at toIndex - 1.
*
* @param array the array being sliced.
* @param fromIndex the starting (inclusive) index.
* @param toIndex the ending (exclusive) index.
*/
public Slice(final E[] array,
final int fromIndex,
final int toIndex) {
checkArray(array);
checkIndex
Solution
public Slice(final E[] array) {For me, adding
final to arguments means lengthening the argument list for hardly any gain, YMMV./**
* Constructs a slice representing everything starting at index
* fromIndex.
*
* @param array the array being sliced.
* @param fromIndex the starting index.
*/
public Slice(final E[] array, final int fromIndex) {
this(array, fromIndex, array.length);
}You've commented it well, but this is pretty unexpected to me. Compare with
substring and similar methods which stretch to the end. I'd suggest to drop all constructors but one, make it private and add fatory methods clearly stating what they create.public Slice(final E[] array,
final int fromIndex,
final int toIndex) {It's a bit strange to accept
toIndex but work with size internally. You may have a reason.fromIndex -= steps % array.length;
if (fromIndex < 0) {
fromIndex += array.length;
}This may blow when
steps is negative (making fromIndex > array.length). Write a method mod so you can use it likefromIndex = mod(fromIndex + steps, array.length);or something like this.
public void moveRight(final int steps) {This should call
moveLeft(-steps). Or better be dropped as swamping the user with that many methods does no good.public void expandFront(final int amount) {
checkNotNegative(amount);I'd allow negative amount and do contract.
/**
* Cycles the array range covered by this slice steps steps to
* the left.
*
* @param steps the amount of steps to cycle.
*/
public void cycleLeft(final int steps) {The Javadoc restates the method name, but I'm still having no idea what's cycling.
Summary
I'm too lazy to go through all of it, but what I dislike most is the amount of methods. Other than that it's nice.
Code Snippets
public Slice(final E[] array) {/**
* Constructs a slice representing everything starting at index
* <code>fromIndex</code>.
*
* @param array the array being sliced.
* @param fromIndex the starting index.
*/
public Slice(final E[] array, final int fromIndex) {
this(array, fromIndex, array.length);
}public Slice(final E[] array,
final int fromIndex,
final int toIndex) {fromIndex -= steps % array.length;
if (fromIndex < 0) {
fromIndex += array.length;
}fromIndex = mod(fromIndex + steps, array.length);Context
StackExchange Code Review Q#90558, answer score: 2
Revisions (0)
No revisions yet.