Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


sort array java

Posted on 2005-04-05
Medium Priority
Last Modified: 2008-03-06
import java.io.*;

import java.util.*;

public class Temp
    static BufferedReader keyboard = new
             BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException
             int[] list = new int[20];

             IntClass length;

             int item;

             StringTokenizer tokenizer;

             System.out.println("Enter  12 integers in one line");

             tokenizer = new StringTokenizer(keyboard.readLine());

             for(int i = 0; i < 12; i++)
                list[i] = Integer.parseInt(tokenizer.nextToken());

             length = new IntClass(12);

             print(list, length.getNum());

             System.out.print("Enter item to be removed: ");
             item = Integer.parseInt(keyboard.readLine());


             print(list, length.getNum());

       public static void remove(int[] list, IntClass len, int removeItem)
             int i;

             int loc = 0;
             boolean found = false;

             if(len.getNum() == 0)
                   System.out.println("Cannot delete from an empty list");
                   for(i = 0; i < len.getNum(); i++)
                         if(removeItem == list[i])
                               found = true;
                               loc = i;

                         for(i = loc + 1; i < len.getNum(); i++)
                               list[i-1] = list[i];

                         len.setNum(len.getNum() - 1);
                         System.out.println(removeItem + " is not in the list");

       public static void print(int[] list, int len)
             for(int i = 0; i < len; i++)
                   System.out.print(list[i] + " ");

my question is if i want to find and delete all occurences of removeItem in the array
how my statments will be.
Question by:georges_nassar
1 Comment
LVL 21

Accepted Solution

MogalManic earned 750 total points
ID: 13707254
The key to removing from an array, is to search BACKWARDS.  Like this:
  for(i = len.getNum(); i>0;i--) {
      if (list[i]==removeItem) {
          if (i<len.getNum()-1)  //Not already last item in the list
             System.arraycopy(list, i+1, list, i, len.getNum()-i);
          len.setNum(len.getNum() - 1); //Make list 1 item smaller

When each item is found, copy the tail end of the array over the item to be deleted and decrement the length by 1

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question