0

In Java, The teacher taught us how to remove an element from an array without using array utils and so on. So I tried the method he gave to us. It updates the index value exactly as I want. after it changes the index value, I want it to delete the last index, "sizeOfArray-1" but I couldn't do that! Any help?

Here the code:

import java.util.Scanner;

public class Arrays {

static int x[] = { 1, 2, 3, 4, 5, 6, 7 };

static Scanner input = new Scanner(System.in);

public int search(int target) {
    for (int index = 0; index < x.length; index++) {
        if (x[index] == target)
            return index;
    }
    return -1;
}

public void deleteIndex(int target) {
    int deleted = search(target);
    if (deleted == -1)
        System.out.println("Entry Not Found!");
    else {
        x[target] = x[7-1];
    }
}

public static void main(String[] args) {
    Arrays f = new Arrays();
    int counteri = 0;
    int counterj = 0;

    for (int j = 0; j < x.length; j++) {
        System.out.print(counterj + "=>" + x[j] + "  \n");
        counterj++;
    }

    f.deleteIndex(input.nextInt());

    for (int i = 0; i < x.length; i++) {
        System.out.print(counteri + "=>" + x[i] + "   \n");
        counteri++;
    }
}
}
6
  • It's not a good idea to create new classes with the same name as existing ones i.e. Arrays already exists. Commented Dec 10, 2014 at 20:24
  • 2
    It is very easy: You cannot change the size of an array. Commented Dec 10, 2014 at 20:25
  • Create a new array with the specified size you want and fill er' back up! Commented Dec 10, 2014 at 20:26
  • 1
    Use an ArrayList.The capacity of an ArrayList van be changed. Commented Dec 10, 2014 at 20:26
  • Best bet would be to create a new array with one less element, and then use a loop to copy all data from the existing array into the new one, but skip the specified index (that you want to remove) Commented Dec 10, 2014 at 20:29

4 Answers 4

1

First of all you have to change this line

x[target] = x[7-1];

to this :

x[deleted] = x[7-1];

because you find an element in your search function, and return its index to deleted so you have to do your action in x[deleted] not x[target]

Your code just replace the actual value of element with amount of last element in here :

else {
    x[target] = x[7-1];
}

So when you want to (so as you call it) delete the last element it just replace last element with it self so it didnot do anything.

You can just simply assign another value that doesnt exist in your array for instance -1 and you could see your function works as you want.

a thing like this :

else {
    x[deleted] = -1;
}

But it is not delete actually, and you cant delete items of array in java.

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

2 Comments

@Chance if it is your answer, please remember to mark it as answer
Thank you very much @Ali Amiri, Your answer was very helpful.. I added: int size = x.length; size--;
1

You really cannot delete an item from an array in Java. Here is some pseudo code that shows what you can do instead:

  • create a new array that has size -1 of the original array
  • start looping, keep track of the current index
  • copy the item(s) from the original array to the new array corresponding to the current index if it should be kept (otherwise skip the item that should be removed)
  • return the new array

1 Comment

I will add to this, as the asker appears to be a beginner: use Arrays.copyOf().
0

An array in Java has fixed predefined length, once you initialize the array you cannot actually remove an element from it. The best thing you can do is to create another array containing all the elements of the original array without that specific one.

Comments

0
//delete the element the perticular element in a position//
import java.util.*;
class main13
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the range");
        int no=sc.nextInt();
        System.out.println("Enter the array elements");
        int a[]=new int[no];
        int i,j;

        for(i=0;i<no;i++)
        {
            a[i]=sc.nextInt();
        }
        System.out.println("Enter the element you want to delete");
        int d=sc.nextInt();
        for(i=0;i<no;i++)
        {
            if(d==a[i])
            {
                for(j=i;j<no-1;j++)
                {
                    a[j]=a[j+1];
                }

                break;
            }
            else
            {
                System.out.println("Element not found");
                System.exit(0);
            }
        }
        System.out.println("After deletion:");
        for(i=0;i<no-1;i++)
        {
            System.out.println(a[i]);
        }
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.