Java ArrayIndexOutOfBoundsException [duplicate] - java

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.

Related

In what version of Java did assigning false to an integer become invalid? [duplicate]

This question already has an answer here:
why would you want to declare a true false variable as type int?
(1 answer)
Closed 2 years ago.
I have the following legacy code from what looks to be a Java 1.1 library:
int colon_index = false;
for(int i = 0; i < params_split.length; ++i) {
int colon_index = params_split[i].indexOf(":");
if (colon_index > 0) {
// ...
}
}
It appears to be assigning false to a variable of type int. This is decompiled code, so it's also possible the IntelliJ decompiler has made a mistake.
I've checked release notes for old versions, but haven't been able to spot this change yet.
Assuming this was correct at some point in Java's history, in what version of Java did this syntax stop being valid?
You can't trust decompiled code. Originally, it would've been int colon_index = 0
Check out this answer for more details.

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

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");
}
}

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.

What is value:while{} mean in Java? [duplicate]

This question already has answers here:
Please explain the usage of Labeled Statements
(3 answers)
Java Label usage [duplicate]
(2 answers)
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Should I avoid using Java Label Statements?
(12 answers)
Closed 4 years ago.
I've read some tutorials which using this syntax in Java, but I don't know what it's mean?
newNumber:while (1 <= 500) {
// do something
}
I don't understand what newNumber:while mean and I can't find it on oracle documentation.
newValue is label here. You can use it with break and continue statement. It becomes relevant for nesting loops when you want to jump from inside the nested loop.

Error with array initialization [duplicate]

This question already has answers here:
How is this illegal
(5 answers)
Array error in java [duplicate]
(3 answers)
Error in simple array... How to fix [duplicate]
(1 answer)
Closed 5 years ago.
New to Java, this is part of a practice question from a question in my book where i'm learning java. I think I've traced the error to the array, in particular to the initialization but i'm not sure how to fix it, why isn this correct?
int[][][] arr;
arr= new int[20][][];
arr[0] = new int[1][];
arr[0][0] = new int[10]{1,1,-1,-1,-1,1,-1,-1,1,-1};
When you supply an array initializer expression, you can't specify the array dimensions too.
You can use :
arr[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1};
When i run it in eclipse it says Cannot define dimension expressions when an array initializer is provided., i think that's really clear for an error message. It means you can either specify the dimensions or the initialize the array. But NOT both at the same time.
Change to:
inputs[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1};

Categories

Resources