I'm trying two write a method that each element in the new array corresponds to the difference of two adjacent numbers in the argument list. And the method should return a new array. I have written the method below but I have got this error Index 0 out of bounds for length 0
.
int [] diff(int [] arr){
int element;
int [] newArr = new int[0];
for(int i = 0; i < arr.length; i ){
element = arr[i] - arr[i 1];
newArr [i] = element;
}
return newArr;
}
CodePudding user response:
the for loop i should be like less than arr.length-1 as you are accessing arr[i 1] so on last iteration i 1 becomes out of bounds.
CodePudding user response:
By subtracting the elements of an array with n
elements from each other, an array with n-1
elements is obtained.
class Test {
public static void main(String[] args) {
// The original array contains n elements.
int[] original = new int[] {4, 2, 6, 1, 9, 5};
// The resulting array contains n-1 elements.
int[] result = new int[original.length - 1];
result = diff(original);
print(result);
}
public static void print(int[] array ) {
for(int i = 0 ; i < array.length ; i) {
System.out.println("Array[" i "]: " array[i]);
}
}
public static int [] diff(int [] array){
// The resulting array contains n-1 elements.
int[] newArray = new int[array.length - 1];
for(int i = 0; i < array.length - 1; i ){
newArray[i] = array[i] - array[i 1];
}
return newArray;
}
}
The result:
Array[0]: 2
Array[1]: -4
Array[2]: 5
Array[3]: -8
Array[4]: 4
CodePudding user response:
In your code newArr initialise to 0 length. That's why it returns index out of bound when adding element to it. Try below code,
int[] diff(int []arr){
int element;
int [] newArr = new int[arr.length-1];
for(int i = 1; i < arr.length; i ){
element = arr[i-1] - arr[i];
newArr [i-1] = element;
}
return newArr;
}
CodePudding user response:
If you look with more precision your code you'll see an obvious error. Don't worry with time you won't do that error again ;)
int [] diff(int [] arr){
int element;
int [] newArr = new int[0];
for(int i = 0; i < arr.length; i ){
element = arr[i] - arr[i 1];
newArr [i] = element;
}
return newArr;
}
Just look your loop if I translate that in english it will mean "For Each i from 0 to the length of the array"
element = arr[i] - arr[i 1];
The final i-th term will be arr.length-1. So why you check the arr[arr.length] ? In an array of length n the indice is 0,1,...,n-1. Not n
CodePudding user response:
For any array of size N
, there will be N-1
differences. Here is a Streams
solution that provides the required result.
int[] s = {10,8,2,9,3,4};
int[] result = diff(s);
System.out.println(Arrays.toString(result));
prints
[2, 6, -7, 6, -1]
- Iterates over the array and places the adjacent value differences on the stream. Then collects them in an array.
- A valid array of
length >= 2
is required, otherwise an exception is thrown.
public static int [] diff(int [] arr) {
if (arr == null || arr.length < 2) {
throw new IllegalArgumentException("Invalid array for method.");
}
return IntStream.range(0, arr.length-1)
.map(i->arr[i] - arr[i 1])
.toArray();
}
}
CodePudding user response:
Your code contains three errors.
int [] newArr = new int[0];
i < arr.length
arr[i] - arr[i 1];
Below is an updated version of your code to address those three problems.
import java.util.*;
public class Main{
static int [] diff(int [] arr){
int element;
int [] newArr = new int[arr.length-1];
for(int i = 0; i < arr.length-1; i ){
element = Math.abs(arr[i] - arr[i 1]);
newArr [i] = element;
}
return newArr;
}
public static void main(String[] args) {
int[] a = {1,5,8,15};
System.out.println(Arrays.toString(diff(a)));
}
}
Output:
[4, 3, 7]