I need some help to identify why this code is throwing an ArrayIndexOutOfBoundsException:
public class palindrome
{
public static void main(String[] args)
{
String name = "Michael Knight";
char ch[] = name.toCharArray();
int size = name.length();
for(int i = 0; i<size; i++) {
//System.out.println(size);
System.out.println(ch[i]);
for(int j=size; j>=0; j--) {
System.out.println(ch[j]);
}
}
}
}
ch[j] is out of bounds when j==size, since the indices go from 0 to size-1.
It should be :
for(int j=size-1; j>=0; j--){
System.out.println(ch[j]);
}
In the for loop, the j starts at index j=size which is already out of array bounds since the array starts from 0 to size - 1, thus ch[j] throws an ArrayIndexOutOfBoundsException.
The correct way to loop in reverse should be:
for(int j= (size - 1); j>=0; j--){
System.out.println(ch[j]);
}
try like this;
public class palindrome{
public static void main(String []arg$){
String name = "Michael Knight";
char ch[] = name.toCharArray();
int size = name.length();
for(int i = 0; i<size; i++){
//System.out.println(size);
System.out.println(ch[i]);
for(int j=size-1; j>=0; j--){
System.out.println(ch[j]);
}
}
}
}
for(int j=size; j>=0; j--) {
System.out.println(ch[j]);
j=size is out of bounds .
The last index of array is size-1.
j should be initialized with the last index of array in this case.
Related
getting output as 0123456789 but i want to store it in reverse order . i know i am missing something please help. So the order would be in 9876543210
for(int i = a.length-1; i>=0 ; i--){
a[i] = i;
}
Use a[a.length-i-1] = i inside your loop.
You're approach sets i element to be i, instead of the reverse which would be length-i-1
You want it in reverse order so first value should go into last array element.
You should do it like shown below in snippet:
public static void main(String... args) {
int a[]=new int[10];
for(int i = a.length-1; i>=0 ; i--){
a[a.length-i-1] = i;
}
System.out.println(Arrays.toString(a));
}
Just change your code like below
for(int i = 0; i<a.length ; i++){
a[i] = a.lenght-i-1;
}
Create the new array and store the reversed value in the new array.
try this:
import java.util.*;
public class Main
{
public static void main(String []args){
int[] a = {0,1,2,3,4,5,6};
int[] b = new int[a.length];
int j=0;
for(int i = a.length-1; i>=0 ; i--){
b[j] = a[i];
j++;
}
System.out.println(Arrays.toString(b));
}
}
This should reverse an array:
for(int i = 0; i <= a.length / 2; i++){
int tmp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = tmp;
}
I am printing Sorted Array elements using Selection Sort. But I am getting my input array elements as output in same sequence without sorting.
public class SelectionSort {
public static void main(String[] args) {
int[] arr= {1,9,3,0,7};
int n=arr.length;
for(int i=0; i<n-1; i++)
{
int minimumIndex = i;
for(int j=i; j<n; j++)
{
if(arr[j]<arr[minimumIndex])
{
minimumIndex=j;
}
}
int temp=arr[i];
arr[i]=arr[minimumIndex];
arr[i]=temp;
}
for(int e: arr)
{
System.out.print(e+" ");
}
}
}
Expected o/p : 0 1 3 7 9
Actual o/p: 1 9 3 0 7
In Your method code, the actual problem is swapping elements,
the Sequence needs to be like this as below,
int temp=arr[minimumIndex];
arr[minimumIndex]=arr[i];
arr[i] =temp;
instead of
int temp=arr[i];
arr[i]=arr[minimumIndex];
arr[i]=temp;
There are two issues I see. One is the way you are swapping the items. You need to replace the item where you found the minimum index. Also, your J index should start one after your I index. You can assume that the one before it is the smallest as you are looping through. I have changed a few pieces of the code and tested it and it works fine for me.
for (int i = 0; i < arr.length - 1; i++)
{
int minimumIndex = i;
for (int j = i + 1; j < arr.length; j++)
{
if (arr[j] < arr[minimumIndex])
{
minimumIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minimumIndex];
arr[minimumIndex] = temp;
}
public class InsertionSort{
public static void main(String [] args){
int [] a = {45,23,4,6,2};
for(int i = 0; i< a.length; i++){
for(int j = i; j>0; j--){
if(a[j]< a[j-1]){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
System.out.println(a[j]);
}
}
}
}
}
output:
45
45
23
45
23
45
23
6
4
I would like it to be in ascending order.
Your array is already sorted, you just have to move the print statement out of the loops
public static void main(String [] args){
int [] a = {45,23,4,6,2};
for(int i = 0; i< a.length; i++){
for(int j = i; j>0; j--){
if(a[j]< a[j-1]){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
// Arrays.stream(a).forEach(System.out::println); -- Java 8
for (int idx = 0; idx < a.length; idx++) {
System.out.println(a[idx]);
}
}
you have to print the whole array after processing, not in the loop. Remove the print staement from the loop and put outside the loop.
Your code is correct. However, there is an issue with the way you are printing the elements. Remember that your array is sorted only after the first for loop completes but you are printing a[j] during sorting process. At this point the elements are not at all sorted and thus you are getting wrong output. So you remove that print statement from the second for loop and use another for loop to print after sorting is over.
public class InsertionSort{
public static void main(String [] args){
int [] a = {45,23,4,6,2};
for(int i = 0; i< a.length; i++){
for(int j = i; j>0; j--){
if(a[j]< a[j-1]){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
// System.out.println(a[j]);
}
}
}
//print array ascending order
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
I have following code in Java.
public class TestArray
{
public static void main(String s[])
{
int arr[]={23,12,1,4,1,4,23,6};
int temp=arr[0];
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
if(arr[i]>arr[j+1])
{
temp=arr[j+1];
arr[j+1]=arr[i];
arr[i]=temp;
}
if(arr[i]==arr[j+1])
{
arr[j+1]=arr[j+2];
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
But this code is throwing
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at com.test.java.TestArray.main(TestArray.java:16)
Could anyone help me with this ?
An attempt to index arr[j + 1] or arr[j + 2] will fail when j is arr.length - 1.
Should you run your indexes to one less than you currently do? Also, that final flourish where you index j + 2 looks unnecessary.
As a style point, keep the scope of temp as tight as possible. You only need it when you swap two array elements.
Looks like you are trying to sort the array in descending order. You can try this:
public class TestArray
{
public static void main(String s[])
{
int arr[]={23,12,1,4,1,4,23,6};
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] < arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
You can understand the code.
If you are wondering how you got exception, refer answer by Bathsheba
Modify your code into
for(int i=0;i<arr.length-1;i++) {
for(int j=i;j<arr.length-1;j++) {
if(arr[i]>arr[j+1]) {
temp=arr[j+1];
arr[j+1]=arr[i];
arr[i]=temp;
}
if(arr[i]==arr[j+1]) {
arr[j+1]=arr[j+2];
}
}
}
In your loop, value of j will be from 0 to 7. And you are trying to access the j+1'th position of array. So when j has value 7 you are accessing a[8] and it is not within the bounds of the array size. An array of size 8 will have index values from 0 to 7.
What would be the last valid index for this array?
double[][] array = new double[11][17];
The answer is [10][16], let's see how in code.
public static void main(String[] args) {
double[][] array = new double[11][17];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.printf("[%d][%d]%n", i, j);
}
}
}
And the last line of output is
[10][16]
Or, you could remember that array indexes start at 0 and the last element of an array is at the length of the array - 1.