I have an array which has null elements also. I want to keep track of number of elements that have the contents in it.
int counter = 0;
for (int a = 0; i <=array1.length; a ++){
if (array1[a] != null)
counter ++;
}
I used the above code for it. But I am getting ArrayIndexoutofBound exception at line
if (array1[a] != null)
why is that? Can someone point it pls.
Replace
for (int a = 0; i <=array1.length; a ++){
by
for (int a = 0; i <array1.length; a++){
Your code fails on element array1[array1.length], because the index must be between 0 and array1.length - 1 inclusive.
array1.length is the size of the array, but since the index is 0-based, you can only go to array1.length-1. So, either change your a <= array1.length to a <= array1.length - 1 or change it to the more common a < array1.length.
Change
for (int a = 0; i <=array1.length; a ++){
into
for (int a = 0; i <array1.length; a ++){
There is no element array1[array1.length]
If you have size of 5 array means you have index from 0 to 4, In other word index of the first element is 0 and the index of last element is 4
Of course it is because you are iterating one time extra than the length of an array.
If the length of your array is 3. Your array stores elements like array[0], array[1] and array[2].
Index of an array or any list Map Set starts from 0.
Use for(int a = 0; a < array1.length;a++)
In your "for" loop,it should be "a<=" instead of 'i'
switch <= to < in your for loop
Related
I have a basic question about the inner loop length in Java selection sort. Here is the commonly used code for selection sort:
package com.java2novice.algos;
public class MySelectionSort {
public static int[] doSelectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++)
/* why not the length is not arr.length-1? I think the length is
exactly the same as what it is, the array's length is a
static number as array.length-1, but when it comes to the inner
loop why it plus 1 to the array length? */
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
}
Imagine you're trying to sort five items. The array length is 5, which means the indexes are 0 through 4.
In the first iteration of the inner loop, you have i=0 and j=1. You need j to index to the end of the array. That's why the expression in the inner loop is j < array.Length.
i, of course, only goes from 0 to 3, which is why the outer loop has i < array.Length - 1.
If you lay five playing cards on a table and walk through the steps of the selection sort, you'll get a better idea of what's happening here.
the first loop does not need to check the last index, therefore, it goes to arr.length - 1. on the second loop, of course, the last index of the array must be checked so the loop goes to the last index (arr.length).
imagine if the first loop goes to the last index. then this line for (int j = i + 1; j < arr.length; j++) will never execute.
check out this Pseudo code of selection sort for a better understanding of the algorithm
First of all, keep in mind I am new at this, so smart guys, be nice!
I am trying to understand why in this line:
for(i = word.length() -1; i >= 0; i--)
System.out.print(word.charAt(i));
I get my string replied to me backwards fine (answer: oaiCgiiB), and in this line of code:
for(i = 0 ; i <= word.length(); i++)
System.out.print(word.charAt(i));
I get an error...
Beijing ChicagoException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 15
at java.lang.String.charAt(String.java:646)
at p200548.main(p200548.java:43)
I want the second one to work like the first one but instead of output BiigCiao
Any help would be appreciated.
Because you are out of bounds.
While:
for(i = word.length() -1; i >= 0; i--)
System.out.print(word.charAt(i));
for length = 3, uses the indexes 2, 1, 0, the following:
for(i = 0 ; i <= word.length(); i++)
System.out.print(word.charAt(i));
uses the indexes 0, 1, 2, 3. And 3 is out of bounds for length = 3.
You probably want:
for(i = 0 ; i < word.length(); i++)
System.out.print(word.charAt(i));
which would use the indexes 0, 1, 2.
for(i = 0 ; i <= word.length()-1; i++) //add mines 1 here
System.out.print(word.charAt(i));
you have to add -1 to ;word.length()-1;
because word starts at index "0" and ends with "word.length()-1"
In C language you would not get an exception because it does not care about crossing bounds of an array. This is one of the problems in C. Java, however always checks for bounds and gives you a 'IndexOutOfBoundsException' whenever you try to access an index outside array.
An equivalent code in C:
for(i = 0 ; i <= strlen(word); i++)
printf("%c",word[i]);
would work fine as word[length] is '\0' and there is no problem printing it
What is being done in this for loop, counter and arr are two different ArrayLists.
// 3. store count of each number as we iterate through arr
for(int i = 0; i< arr.size(); i++){
counter[arr.get(i)]++;
}
arr contains indexes that the corresponding elements for them on counter should be incremented.
For example:
arr = [1,3,4]
Then the elements in 1,3 and 4 will be incremented in the array counter.
I highly recommend you to debug your code to better understand the flow of the program. You should also be careful with ArrayIndexOutOfBoundsException:
for(int i = 0; i< arr.size(); i++) {
if(arr.get(i) < 0 || arr.get(i) >= counter.length) {
continue;
}
counter[arr.get(i)]++;
}
Or doing something else, depends on the logic of your program.
If arr contains integer numbers it is index of counter array. Then you are incrementing the value of counter array on arr.get(i) index. I hope you understand what I am saying.
I am currently trying to take the elements of an array and reverse its order in Java. How come I cannot print the elements of the array by counting downwards using a for loop without changing the actual ordering of elements in my array?
private void printArray(int[] array) {
for (int i = array.length; i >= 0; i--){
println(array[i]);
}
}
Array indices start at 0 and end at array.length - 1. Here, you get an ArrayIndexOutOfBOundsException since your first read is past the end of the array (int i = array.length;).
Do:
for (int i = array.length - 1; i >= 0; i--)
println(array[i]);
Try
for (int i = array.length - 1; -1 != i; --i){
As indexes start from 0
I'm trying to populuate an array with random numbers to be sorted and used in a binary search. All of my code seems to work fine except the generating part. The numbers need be between 1-32767 and I continue to get 0.
for(int i = 0; i < SIZE-1; i++){
array[i] = (gen.nextInt(32767 - 1) + 1);
}
// Print out five entries
for(int i = 0; i < 5; i++){
System.out.println(array[i]);
}
// Sort array
Arrays.sort(array);
// Print out first five sorted entries
for(int i = 0; i < 5; i++){
System.out.println(array[i]);
}
After they're sorted and printed, the first entry is always 0. Perhaps this has to do with the array sorting, and I'm not realizing it. Any suggestions?
You're never setting the last element of the array -- use i < SIZE, not i < SIZE-1.