
Hi,
I want to insert an elements inside an array. I have one array1 of size 5(where all teh indexs are full).I have created one more array2 of size 6 and inserted all the elemnts of array1 inside array2. Now I want to insert an element in 3rd postion of array2 such that elements present in 3rd position will move to 4th, 4th will move to 5th and 5th will move to 6.
I have developed a program below. But getting string index out of bound exception. Please help me to fix this issue.
package com.lara;
public class Array1 { public static void main(String[] args) { int a[]= {1,2,3,4,5};
System.out.println("Normal Array:");
for(int k:a)
{
System.out.println(a[k-1]);
}
System.out.println("-------------");
int b[]=new int[6];
System.out.println("Length of B is:"+b.length);
System.out.println("---------------");
b=a;
for(int k:b)
{
System.out.println(b[k-1]);
}
System.out.println("-------xys----");
for(int i=5;i>=0;i--)
{
if(i>2 && i<5)
{
b[i+1]=b[i];
}
b[2]=30;
}
for(int j:a)
{
System.out.println(a[j-1]);
}
} }
Regards: Akash
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.