0

I tried to insert a number into an unordered array, but it has some logical errors. used code is,

    int NoOfItems = 5 ;
    int MyArray[] = {1,6,3,9,5};
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a Number:");
    int m_Number = input.nextInt(); 
    if (NoOfItems < MyArray.length) {
        MyArray[NoOfItems] = m_Number;
        NoOfItems = NoOfItems + 1;
    }
    System.out.println(Arrays.toString(MyArray));

Please help me to fix this.

1
  • Would be better if you would include what errors you got and how you tried to solve it... Commented May 7, 2016 at 5:43

5 Answers 5

1

Your MyArray array is of length 5, so it can hold objects in indexes 0-4. You are trying to insert an object in index 5 which will give you an ArrayIndexOutOfBounds exception. Please note that Arrays in Java have immutable size.

You need to create a new array of size 6 and copy the old array's content into it and then add 5th index. Or else, you can use ArrayList

Sign up to request clarification or add additional context in comments.

Comments

0

Size of arrays are fixed in java, best thing you can do is to use ArrayList.

But if you want to implement this with arrays, here how it works:

int myArray[] = {1, 6, 3, 9, 5};
Scanner input = new Scanner(System.in);
System.out.println("Enter a Number:");
int mNumber = input.nextInt();

// Allocate new array
int newArray[] = new int[myArray.length + 1];
// Copy contents
System.arraycopy(myArray, 0, newArray, 0, myArray.length);
// Put a new number into last cell in array
newArray[newArray.length - 1] = mNumber;

myArray = newArray;

System.out.println(Arrays.toString(myArray));

Comments

0

To answer your question :

it has some logical errors

the code inside the if condition never executes as

"NoOfItems" is 5 and "MyArray.length" returns 4.

condition is 5<4

which never becomes true therefore you never add an element into the array. If you correct your condition and try to add an element into the array you would get a "java.lang.ArrayIndexOutOfBoundsException".

And like the rest of the answers state if your want a re-sizable array in java go for arrayList. Array's are of fixed length in java.

Comments

0

Arrays in Java are fixed size. Their size cannot be modified after creation. If you want dynamic length arrays, consider using java.util.ArrayList

Your example in online java IDE

Comments

0

Your array has a fix value when you created it. You can just change the values in and cannot add news.

You should do that :

ArrayList<Integer> MyArray = new ArrayList<Integer>(); //create a new list
    MyArray.add(1); // put 1 at index 0.
    MyArray.add(6); // put 6 at index 1.
    MyArray.add(3); // ...
    MyArray.add(9); // ...
    MyArray.add(5); // ...
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a Number:");
    int m_Number = input.nextInt(); 
    MyArray.add(m_Number); //put the entered number at the end of MyArray

    System.out.println(MyArray);

Tadaa !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.