Why am i getting this exception error ? ArrayOutOfBounds? [duplicate] - java

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 3 years ago.
I'm trying to check if the integers in an array are increasing. But I'm getting ArrayOutOfBounds exception, and I do not understand why. I'm new to programming.
for (int i =0; i > newArray.length ; i++){
if (newArray[i] > newArray[i+1]){
System.out.println("Not increasing");
}
}
Thank you for your help.

the ArrayOutOfBounds exception happens when you are trying to access a non-existent position in the array.
The problem in your case is the newArray[i+1] which tries to access a position non-existent in your array.
I recommend editing the condition i < newArray.length-1 : There are, of course, other ways to fix this but this is the easiest.

Let’s say your array length is 8 so the positions are from 0 to 7
In the last run of the for loop i=7 and i+1 =8
You don’t have newArray[i+1]
You should put the first if inside a new if
if(i<(newArray.length - 1)){
if (newArray[i] > newArray[i+1]){
System.out.println("Not increasing");
}
}

Related

Error in an array, java.lang.NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
So, i'm using an array in my program, and it gives me this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
The error is in fourth line of this code:
int n = numbers.length, numBigger10 = 0;
for (int i = 0; i < n; i++)
if (numbers[i] > 10)
numbersBigger10[i] = numbers[i];
numBigger10++;
What can I do to solve?
If you want to know more details about the error please feel free to ask.
The reason you are getting this NullPointerException is because in your if statement you are trying to assign numbers[i] to an index location that is not present in the numbersBigger10 array.
i.e. The two arrays, numbers and numbersBigger10 have different sizes.
edit - It would be helpful to see more code though, specifically the part where you declared numbersBigger10.

Java: How to get the index of the maximum element in an integer ArrayList? [duplicate]

This question already has answers here:
LinkedList : Collections.max() throwing NoSuchElementException
(3 answers)
Closed 4 years ago.
I read about the indexOf and .max methods and tried to do this
int indexOfMax =list.indexOf(Collections.max(list));
But I get a NoSuchElementException. How else could one go about this?
Your list is empty.
According to the docs the mentioned exception happens when the collection is empty

What does this lean of code mean? [duplicate]

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
capacity = (cap > 0) ? cap : CAPACITY;
I'm just looking through my lecture notes, and I can't figure out what this line of code does. Can someone help me?
It's called conditional expression and in your case it means that if cap is greater than 0, the capacity variable will get the value "cap", if false then it will get the value "CAPACITY".

Java ArrayIndexOutOfBoundsException [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 5 years ago.
I wrote a Java program below:
int[] tar = {1,2,5};
for(int i=0 ; i<tar.length ; i++)
{
if(tar[i] - tar[i-1] > 2)
{
System.out.print("true");
}
}
why "tar[i] - tar[i-1]" doesn't mean any error? Isn't an error of ArrayIndexOutBoundsException?
It is an Exception and not error (if you mean it by compiler), cause Java knows about your array values only at run time.
Compiler cannot run your code and see that. What if at runtime you provided some good values at right indices ? That is the reason, compiler only checks the compiler level rules. It won't hint you about runtime behaviour.

What's the difference between --i and i--? [duplicate]

This question already has answers here:
Post-increment and pre-increment within a 'for' loop produce same output [duplicate]
(12 answers)
Semantics of pre- and postfix "++" operator in Java [duplicate]
(7 answers)
Closed 9 years ago.
In some code i've seen a for loop with a --i as third parameters
for(int i=array.length; i<0; --i)
Maybe someone can explain me the difference with i-- ?
i guess it's something like the moment when i is decremented ?
If, for example, i = 5:
--i decrements i by 1 then gives you the value of i (4).
i-- gives you the value of i (5) then decrements it by 1.
Both will give you the same result in a for loop.

Categories

Resources