georges_nassar
asked on
sort array java
import java.io.*;
import java.util.*;
public class Temp
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.i n));
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.r eadLine()) ;
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: ");
System.out.flush();
item = Integer.parseInt(keyboard. readLine() );
System.out.println();
remove(list,length,item);
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");
else
{
for(i = 0; i < len.getNum(); i++)
if(removeItem == list[i])
{
found = true;
loc = i;
break;
}
if(found)
{
for(i = loc + 1; i < len.getNum(); i++)
list[i-1] = list[i];
len.setNum(len.getNum() - 1);
}
else
System.out.println(removeI tem + " 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] + " ");
System.out.println();
}
}
my question is if i want to find and delete all occurences of removeItem in the array
how my statments will be.
import java.util.*;
public class Temp
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.i
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.r
for(int i = 0; i < 12; i++)
list[i] = Integer.parseInt(tokenizer
length = new IntClass(12);
print(list, length.getNum());
System.out.print("Enter item to be removed: ");
System.out.flush();
item = Integer.parseInt(keyboard.
System.out.println();
remove(list,length,item);
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
else
{
for(i = 0; i < len.getNum(); i++)
if(removeItem == list[i])
{
found = true;
loc = i;
break;
}
if(found)
{
for(i = loc + 1; i < len.getNum(); i++)
list[i-1] = list[i];
len.setNum(len.getNum() - 1);
}
else
System.out.println(removeI
}
}
public static void print(int[] list, int len)
{
for(int i = 0; i < len; i++)
System.out.print(list[i] + " ");
System.out.println();
}
}
my question is if i want to find and delete all occurences of removeItem in the array
how my statments will be.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.