# Need help with inserting a new number into an array

• 01-21-2011, 02:51 AM
Get_tanked
Need help with inserting a new number into an array
I have to write some code that will take an array of positive integers and look at each index. The rules are that once the program finds a zero it will stop looking at the array and only use the numbers before the zero was found. For example the numbers I use in the array are 1,2,3,4,0 it would have look at 1,2,3,4. I also have to write some methods to go along with it. The part that I cant seem to figure out is a method called insert, where it will try to insert an integer into the array if the integer isnt already in the array. The inserted number has to trade places with the zero and push the zero back one index. For example, I wanted to insert a 7 into 1,2,3,4,0 it should come out as 1,2,3,4,7,0 but for some reason ever time no matter what I try the 7 is completely ignored. I've almost finished the code but for some reason when I try to insert the 7 it wont insert, even though it looks as though my code is correct (at least according to me :p). I would really appreciate if someone could help me out on this its slowly been driving me insane for the past 3 days.

Code:

```import java.util.Arrays; public class Intcolls1 {         private int[] c;                   public Intcolls1(int i)           {               c = new int[i];               c[0] = 0;           }           public void setc() // this is what i used to set the integers into the array. it produces an array {1,2,3,4,0}           {                   int x = 1;                                     for(int i = 0; i < c.length; i++)               {                           c[i] = x;                           x++;                           if(c[i] == 5)                           {                                   c[i] = 0;                           }               }           }                     public void insert(int i) // This is the part i cant figure out where i insert the integer in the array           {                                     int j = 0;                   while((c[j] != 0)&&(c[j] != i)) // finds the zero or if the int being inserted is already in the array                    {                                 j++;                   }                   if (c[j] == i)                   {                                 System.out.println("The number is already in the array");                   }                   if (c[j] == 0)                   {                           if (j == c.length - 1) // incase my array is starting to go out of bounds and needs to be bigger                           {                                                                             int[] d = new int[c.length * 2]; // creates a new array thats twice to length of the old to make more room                                         for(int k = 0; k < c.length; k++) // copies the old array into the new array                                         {                                                 d[k] = c[k];                                         }                                         c = d; // this is where im having trouble i want to make the c's pointer point at d and get its properties                                   }                           c[j] = i; // These next two lines are the actual insert and dont seem to do anything at all                           c[j++] = 0;                         }                                     }           public void print() // prints the array, when the array prints i get 1,2,3,4,0,0,0,0,0,0 with no 7 in there           {                   for (int i = 0; i < c.length; i++)               {                           System.out.print(c[i]);               }           }             public static void main(String[] args)             {                   int i;                   Scanner kb;                   kb = new Scanner(System.in);                                     Intcolls1 b = new Intcolls1(5);                                           System.out.println("Enter a number to insert to the array");                   i = kb.nextInt();                                   b.setc();                   b.insert(i);                    b.print();             } }```
• 01-21-2011, 03:10 AM
Junky
Does the array need to remain sorted?

If the array is [1 2 4 5 0] and I insert 3, does the array become [1 2 3 4 5 0] or [1 2 4 5 3 0]?
• 01-21-2011, 03:12 AM
Code:

```c[j++] = 0; //should be c[++j] = 0;```
j++ will look at index j, then increment j.
++j will increment j, then look at the new index j!

Code:

`c = d;`
is correct and does what you intend it to, don't worry about it :D
• 01-21-2011, 03:15 AM
I forgot to explain - for example:
Code:

```int[] a = {1,2,0,...} j = 2; //insert 7 a[j] = 7;      //set index 2 to 7 a[j++] = 0; //set index 2 to 0, then increment j to 3```
See why that would fail and ignore 7 every time?
• 01-21-2011, 03:19 AM
Junky
Another clarification needed.

Are there any numbers after the zero? If not then there is no need to do any "pushing". If the array is [1 2 3 0 0 0] and inserting 4 the array become [1 2 3 4 0 0]. All you need ot do is store 4 at the approriate index and nothing else.
• 01-21-2011, 03:23 AM
Get_tanked