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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

762 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