the program should be able to display array (11 22 33 44 55 66 77 88 99 00).
then will ask user to input a number that is (only) in the array displayed.
then display the new array. sample: input = 44, new array should be 11 22 33 55 66 77 88 99 00, '44' is removed.

here's my code so far

import java.util.Scanner;

class ArrayApp
  public static void main(String[] args)
    int[] numbers = {11, 22, 33, 44, 55, 66, 77, 88, 99, 00};

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a value to search: ");
    int val = sc.nextInt();

    // Search the array for val
    boolean found = false;
    for (int i = 0; i < numbers.length; i++) {
      if (numbers[i] == val) {
        System.out.println(val + " is found at index " + i);
        found = true;

    if (!found)
      System.out.println(val + " is NOT found.");


    int[] newAr = new int[numbers.length]; 
    for (int i = 0; i < numbers.length; i++)
      newAr[i] = numbers[i];

    System.out.print("newAr contains: ");

    for (int i = 0; i < newAr.length; i++)
      System.out.print(newAr[i] + " "); 



3 Years
Discussion Span
Last Post by joma.tioquin
Featured Replies

how will i remove what is input by le user? for ex. 44, so the new array will be 11 22 33 55 66 77 88 99 00. thx

11 22 33 44 55 66 77 
 |  |  |   /  /  /
11 22 33 55 66 77

See what's happened: for each entry before 44 you just copy the values, end for each entry after 44 you copy the element one up, ie

before user's number  newAr[i] = numbers[i];
after user's number   newAr[i] = numbers[i+1];

tried. here's what happened

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at ArrayApp.main(ArrayApp.java:34)

That's of limited value when you don't post the code that created the error, but if you have something like

int[] newAr = new int[numbers.length];
for (int i = 0; i < numbers.length; i++)
  newAr[i] = numbers[i+1];

then yes, you will try to access one element off the end of numbers.
Remember that the new array should be one element shorter than the original, then if you use that for the for loop, [i] will go up the last element of newAr and [i+1] will go up the the last element of numbers correctly


yeah, I just added the +1, then there was the error.

so is it like, origarray = 10, newarray =10 then if [i+1]. it becomes 11? that's y out of bounds? o.O
confused, how will i make the newarray shorter? so that if i add 1, it wont be out of bounds?

Edited by joma.tioquin: typo error


Making new array one shorter than nunbers...
newAr = new int[origAr.length -1];
now newAr is 9, origAr is 10, so you can use i to loop through newAr and i+1 for origArr

ps: don't worry, everyone here started out as a noob.

int[] newAr = new int[numbers.length-1]; 
    for (int i = 0; i < numbers.length; i++)
      newAr[i] = numbers[i+1];

sad still error ...

Edited by joma.tioquin: accidentally entered but not yet finished typing


Are you restricted to using an array?, in situations where removal, and insertion are common a LinkedList or ArrayList may be a better option. If your curious as to how I did it with a linked list, here is some sample code that I wrote:

package linkedlistexample;

import java.util.LinkedList;
import java.util.Scanner;
 * @author PBJ
public class LinkedListExample {
    private static Scanner console = new Scanner(System.in);
    public static void main(String[] args) {
         * 1. Create LinkedList 
         * 2. Populate LinkedList (0 - 10)
         * 3. Output LinkedList
         * 4. Request user input (numberToRemove)
         * 5a. Search LinkedList
         * 5b. Compare numberToRemove, with each value in LinkedList
         * 5c. If numberToRemove, is equivalent to number in Linked List, remove
         * 6. Output LinkedList
        int counter = 1;
            LinkedList listOfValues = createLinkedList();                       //1
            listOfValues = populateLinkedList(listOfValues);                    //2
            System.out.println("BEFORE REMOVING");
            outputLinkedList(listOfValues);                                     //3
            int numberToRemove = getUserInput();                                //4
            listOfValues = findValueInLinkedList(listOfValues,numberToRemove);  // 5
            System.out.println("AFTER REMOVING");
            outputLinkedList(listOfValues);                                     // 6
            System.out.println("---ran: " + counter + " times---");

    private static LinkedList createLinkedList() {       
        LinkedList<Object> tempList;
        return tempList = new LinkedList<>();

    private static LinkedList populateLinkedList(LinkedList listOfValues) {
        for(int i = 0; i < 11; i++){
        return listOfValues;

    private static void outputLinkedList(LinkedList listOfValues) {

    private static int getUserInput() {
        System.out.println("Input number to remove from list: ");
        Integer tempNumber = console.nextInt();
        return tempNumber;

    private static LinkedList findValueInLinkedList(LinkedList listOfValues, int numberToRemove) {
        for(int i = 0; i < listOfValues.size(); i++){
            Object num = listOfValues.get(i);
            if(num == numberToRemove){
        return listOfValues;

I hope that it is pretty self explanatory, I tried to make it as straight-forward as possible. Once again, this is just showing that there are other collections that can accomplish the same task.

Votes + Comments

yeah way too advanced XD anyway thanks, that's how the program should be, but, is there any "simpler" code?


the simplest way is to follow jamescherril.

So create and copy all values into a new array except specifying the size too be 1 less. Keep on trying you've almost got it.

Dynamically changing the size of an Array isn't really suited for arrays... As a learning exercise what you're doing is just fine. In the real world I would opt for an ArrayList and use those methods associated with that abstract data structure.


I'd like to add also. You can't remove an item from an array, it is not possible.
You can only change its value to whatever you need or NULL. If the value is null then the key is still there, but it's value is NULL.
When you create an array it's values are placed into a memory block on the system until the end of program execution or a break point.

Edited by Banderson


^ arrays are kept in memory until your program no longer has any references to them, then they are garbage collected.

int[] arrRef = {1,2,3}; // creates an array,  stores a ref to it
arrRef = {2.3.4};       // change ref, now there are no refs to {1,2,3}. it will be garbage collected

Re lists - yes it's much easier to use a List rather than array if yo are going to remove elements etc, but why write your own linked list when the Java API provides a choice of pre-written pre-tested fully-documented fully-supported list implementations, including linkedlists and array-based lists?


what i managed to observe: if array is for ex. array= {1 2 3 4 5 6}, then, if i declare newarray, and iteration for [i], then new array = array[i], if I add +1 inside the bracket, ([i+1], what it does is it adds 1 to the size of array, but if I add outside, ([i]+1 or +2 or whatever), it adds a new element, and adds 1 to the last element, so it new array becomes, 1 2 3 4 5 6 7, 7 is the new element got from 6+1. waaa it's too complicated for me . . .


yeah :)), it's like it's our 3rd meeting then boom, she asked us to create that program XD, need a lot of information XD

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.