Grab a Segment of an Array in Java Without Creating a New Array on Heap

Get only part of an Array in Java?

The length of an array in Java is immutable. So, you need to copy the desired part into a new array.

Use copyOfRange method from java.util.Arrays class:

int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);

startIndex is the initial index of the range to be copied, inclusive.

endIndex is the final index of the range to be copied, exclusive. (This index may lie outside the array)

E.g.:

   //index   0   1   2   3   4
int[] arr = {10, 20, 30, 40, 50};
Arrays.copyOfRange(arr, 0, 2); // returns {10, 20}
Arrays.copyOfRange(arr, 1, 4); // returns {20, 30, 40}
Arrays.copyOfRange(arr, 2, arr.length); // returns {30, 40, 50} (length = 5)

How to get a sub array of array in Java, without copying data?

Many classes in Java accept a subset of an arrays as parameter. E.g. Writer.write(char cbuf[], int off, int len). Maybe this already suffices for your usecase.

Is it possible to copy some of an array's values into another array but with the same reference?

For primitive types this is not possible as arrays store primitive values themselves not references. You need to create your own class which will incapsulate values you need and then use arrays of elements of your custom class.
For classes arrays store references to the objects so in that case you will be able to achieve what you want.

How to grab only a certain row of a two-dimensional matrix in java?

Assuming that row means the horizontal segments of the matrix (as it almost always is):

In a 2 dimensional array in java, the rows are the first index and the columns are the second index.

Basically a two dimensional array is an array of arrays. So

int[][] intArray = new int[10][3];

is actually an array of size 10. Each element in the array is an array in itself of size 3.

Say you have an array of integers

int[][] integerArray; //we have to initialize the array.

then we want to work with the 1st row. We would use:

int[] arr = integerArray[0];

LIMITATIONS

  1. The matrix must be initialized (must have values in cells)
  2. The matrix must have a 1st row

Note: we use integerArray[0] because arrays start at index 0, so the third row would be integerArray[2]

If you're explicitly initializing an Object array in Java, is including new Object[] different than not including it?

I think this might be what the author is referring to.

Since Java 5, we can declare functions with variable argument lists.

public static int newStyleSum(final int... numbers) {
int sum = 0;
for (final int number : numbers) {
sum += number;
}
return sum;
}

They can be used as in:

int s = newStyleSum(1, 2, 3, 4);

This feature is merely syntactic sugar. Internally, an anonymous array is passed to the function.

Before we had this syntax, the above example would have to be written as:

public static int oldStyleSum(final int[] numbers) {
int sum = 0;
for (int i = 0; i < numbers.length; ++i) {
sum += numbers[i];
}
return sum;
}

and called as

int s = oldStyleSum(new int[]{1, 2, 3, 4});  // "second" form

but not as

int s = oldStyleSum({1, 2, 3, 4});  // "first" form (syntax error)

which, even today, is still a syntax error.

And that might indeed be what he is talking about. Java 5 came out in 2004 so for a 2002 book, it makes sense.

The new syntax is more flexible and – importantly – backwards compatible, so we can still do

int s = newStyleSum(new int[]{1, 2, 3, 4});

or, more importantly,

int[] numbers = {1, 2, 3, 4};
int s = newStyleSum(numbers);

if we want to.

Truncate an array without copying it?

An array's length in Java cannot be altered after initialization, so you're forced to make a copy with the new size. Actually, the length parameter of a Java array is declared as final, so it cannot be changed once it's set.

If you need to change an array's size, I'd use an ArrayList.



Related Topics



Leave a reply



Submit