0

In R you can do something like this:

> x = c(1, 222, 333, 4, 5)
> x[c(2, 3)]
[1] 222 333

> in the beginning of line is just a prompt in an interactive session. x is a vector of numeric values, and if you want the 2nd and 3rd element of x, you pass another vector c(2, 3) to index x. This is different from Arrays.copyOfRange in java: you can pass an arbitrary index vector to x, not necessarily in a continuous range, i.e. x[c(1,3,5)]

I am wondering if there is a similar feature in java.

6
  • 8
    Can you explain what's the desired behavior? I don't know R. Commented Nov 17, 2014 at 14:06
  • 1
    If you take a minute to explain what does this mean in R you increase your chance to get response from people that are not familiar with R but familiar with java. Commented Nov 17, 2014 at 14:07
  • 1
    It's still not clear. Are (1,2,3,4,5) the values of the array x? Or are they indices of the array? And what does the output [1] 2 3 mean? Commented Nov 17, 2014 at 14:11
  • x is an array of numeric values, called numeric vector in R. Commented Nov 17, 2014 at 14:13
  • I need to ask, does R have indices from 1 or from 0? The arrays in Java start indices from 0. Commented Nov 17, 2014 at 14:14

2 Answers 2

3

As I understood your question, you could create a method that take a list of indexes as parameters.

For generic arrays, you'll need to use Array.newInstance and cast the resulting array to T[] (thanks and kudos for @Radiodef for pointing this out)

//for lists
static <T> List<T> buildList(List<T> original, int... indexes){
    List<T> finalList = new ArrayList<>();
    for(int index : indexes){
        finalList.add(original.get(index));
    }
    return finalList;
}

//for arrays
static <T> T[] buildArray (T[] original, int... indexes){       
    @SuppressWarnings("unchecked")
    T[] finalArray = (T[])Array.newInstance(original.getClass().getComponentType(), indexes.length);
    for(int i = 0; i < indexes.length; i++){
        finalArray[i] = original[indexes[i]];
    }
    return finalArray;
}

Few notes :

  • For primitive arrays (like int[] or double[]), you'll have to overload the methods, that's why I suggest you to use a List with generics instead.
  • Indexes for arrays in Java are 0 based (starts from 0 to length-1)
  • Don't forget to add additional checks for the method (valid index, original list or index not null)
  • If you use a LinkedList instead of an ArrayList, be aware that #get(int index) is an O(n) operation instead of O(1)

Example of usage :

public static void main(String[] args) {
    Integer[] xArray = {-1, 10, 24, 7};
    List<Integer> xList = Arrays.asList(-1, 10, 24, 7);

    System.out.println(Arrays.toString(buildArray(xArray, 0, 2)));
    System.out.println(buildList(xList, 0, 1, 3));
}

Which outputs:

[-1, 24]
[-1, 10, 7]
Sign up to request clarification or add additional context in comments.

5 Comments

Your generic buildArray for T[] is incorrect. You need to use Arrays.copyOf or Array.newInstance. You can't do (T[])new Object[n] in a context where the array is returned. It will throw an exception.
@Radiodef Why would it throw an Exception?
Because you are casting Object[] to T[]!! Try this: String[] arr = buildArray(new String[]{"a", "b"}, 0, 1);
@Radiodef Hmm you are right, let me rethink this. :)
Just use Array#newInstance as in (T[])Array.newInstance(original.getClass().getComponentType(), indexes.length). Your answer is otherwise good.
0

take a look at Arrays.copyOfRange

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.