I have to write a method , RemoveAll that takes three parameters: an array of integers the length of an array and an integer. Say RemoveItem. The method should find and delete all occurences of removeItem in the array.
I have told the instructor that I am not sure exactly what he wants me to do and I have looked at all the instances of removeItems in the script and still I am clueless.
Can someone explain what I should be doing IN ENGLISH?
8 1924
I have to write a method , RemoveAll that takes three parameters: an array of integers the length of an array and an integer. Say RemoveItem. The method should find and delete all occurences of removeItem in the array.
I have told the instructor that I am not sure exactly what he wants me to do and I have looked at all the instances of removeItems in the script and still I am clueless.
Can someone explain what I should be doing IN ENGLISH?
After we finish wruting this you should see that there is no need for the length of the array to be passed in as well. Now we tackle the philosophical aspects first. Suppose you have the following array
{1, 2, 3, 4} and are asked to remove 2. What should be the result? Is it {1, 0, 3, 4} or {1, 3, 4}? It is probably {1, 3, 4} right. Now java arrays don't shrink or stretch so we will need to create a new array to hold the numbers with the occurrences removed. While we are working on the array we need to know which index has had it's value removed. For this we need something more than an int[] because just setting 0 won't work. We might be asked to remove 0 itself! So here is the deal, we are going to make use of an Integer array in our function. Here is how the first part looks like -
-
public int[] removeAll(int[] numbers, int length, int removeItem) {
-
Integer[] items = new Integer[numbers.length];
-
for(int i = 0; i < numbers.length;i++) {
-
items[i] = numbers[i];
-
}
-
}
-
Do you understand any of this?
okay, so far I am with you. he also put in here that I can assume that the array is unsorted. which I am assuming is that the integers are not in recognizable order and that they are random. So I guess that what needs to be created first is the three parameters, is that correct?
try this code .............
void removeAll(int [] elements,int length,int remove_item)
{
Vector l_temp = new Vector();
if(elements.length < length) return; //Wrong length specification
for(int i=0;i<length;i++)
l_temp.add(new Interger(elements[i]));
Vector l_temp1 = new Vector();
l_temp1.add(new Integer(remove_intem));
l_temp.removeAll(l_temp1);
}
Thanks .......................
I am waiting for ur reply....
a llittle bit of change here ..........
void int [] removeAll(int [] elements,int length,int remove_item)
{
Vector l_temp = new Vector();
if(elements.length < length) return; //Wrong length specification
for(int i=0;i<length;i++)
l_temp.add(new Interger(elements[i]));
Vector l_temp1 = new Vector();
l_temp1.add(new Integer(remove_intem));
l_temp.removeAll(l_temp1);
int l_temp2 = new int[l_temp.size()]
for(int i=0;i<l_temp.size();i++)
l_temp2[i] = l_temp.elementAt(i);
return l_temp2;
}
a llittle bit of change here ..........
void int [] removeAll(int [] elements,int length,int remove_item)
{
Vector l_temp = new Vector();
if(elements.length < length) return; //Wrong length specification
for(int i=0;i<length;i++)
l_temp.add(new Interger(elements[i]));
Vector l_temp1 = new Vector();
l_temp1.add(new Integer(remove_intem));
l_temp.removeAll(l_temp1);
int l_temp2 = new int[l_temp.size()]
for(int i=0;i<l_temp.size();i++)
l_temp2[i] = l_temp.elementAt(i);
return l_temp2;
}
The idea is not to use the vector class' removeAll method but to write one's own version of removeAll like this: -
-
public static int[] removeAll(int[] numbers, int length, int removeItem) {
-
Integer[] items = new Integer[numbers.length];
-
int removed = 0;
-
for(int i = 0; i < numbers.length;i++) {
-
if(numbers[i] == removeItem) {
-
items[i] = null;
-
removed++;
-
}
-
else {
-
items[i] = numbers[i];
-
}
-
}
-
numbers = new int[numbers.length - removed];
-
int index = 0;
-
for(int i = 0; i < items.length;i++) {
-
if(items[i] == null) {
-
}
-
else {
-
numbers[index++] = items[i];
-
}
-
}
-
return numbers;
-
}
This is the code that I came up with using what I got out of the instruction. It will not compile and honestly I have tried to change it every which way I can. I have looked at all your responses and have tried to impliment them into the program with no success. I just can't get it to compile
import java.io.*;
import java.util.*;
public class Assignment5
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException
{
int[] list = new int[20]; //start of array
IntClass length;
int item, index;
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 to be removed
System.out.flush();
item = Integer.parseInt(keyboard.readLine());
System.out.println();
System.out.print("Enter the position of the item to be removed.\n" + "(Position of the first element is 1): ");
System.out.flush();
index = Integer.parseInt(keyboard.readLine());
System.out.println();
insertAt(list,length, item, index);
print(list, length.getNum());
}
public static void insertAt(int list[], IntClass len, int item, int index) //simular to what was suggested
{
if(index <= 0 || index > len.getNum())
{
System.out.println("Position of the item to be removeded is out of range");
if(index <= 0)
System.out.println("Position of the item to be removeded must be positive");
else
System.out.println("The size of the list is " + list.length);
}
else
{
if(index > len.getNum())
{
System.out.println("Item will be removed from the list "
+ "at position " + (len.getNum() + 1));
list[len.getNum()] = item;
len.setNum(len.getNum() + 1);
}
else
{
for(int i = len.getNum() - 1; i >= index - 1; i--)
list[i + 1] = list[i];
list[index - 1] = item;
len.setNum(len.getNum() + 1);
}
}
}
public static void print(int[] list, int len)
{
for(int i = 0; i < len; i++)
System.out.print(list[i] + " ");
System.out.println();
}
}
after some munipulation I have an error on line 33 that states array diminsion missing
line 33 print(list, length.getNum(12));
Any clues
This is the code that I came up with using what I got out of the instruction. It will not compile and honestly I have tried to change it every which way I can. I have looked at all your responses and have tried to impliment them into the program with no success. I just can't get it to compile
import java.io.*;
import java.util.*;
public class Assignment5
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException
{
int[] list = new int[20]; //start of array
IntClass length;
int item, index;
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 to be removed
System.out.flush();
item = Integer.parseInt(keyboard.readLine());
System.out.println();
System.out.print("Enter the position of the item to be removed.\n" + "(Position of the first element is 1): ");
System.out.flush();
index = Integer.parseInt(keyboard.readLine());
System.out.println();
insertAt(list,length, item, index);
print(list, length.getNum());
}
public static void insertAt(int list[], IntClass len, int item, int index) //simular to what was suggested
{
if(index <= 0 || index > len.getNum())
{
System.out.println("Position of the item to be removeded is out of range");
if(index <= 0)
System.out.println("Position of the item to be removeded must be positive");
else
System.out.println("The size of the list is " + list.length);
}
else
{
if(index > len.getNum())
{
System.out.println("Item will be removed from the list "
+ "at position " + (len.getNum() + 1));
list[len.getNum()] = item;
len.setNum(len.getNum() + 1);
}
else
{
for(int i = len.getNum() - 1; i >= index - 1; i--)
list[i + 1] = list[i];
list[index - 1] = item;
len.setNum(len.getNum() + 1);
}
}
}
public static void print(int[] list, int len)
{
for(int i = 0; i < len; i++)
System.out.print(list[i] + " ");
System.out.println();
}
}
The method that I posted worked on my compiler. Here is how to test it -
-
import java.util.*;
-
public class Test {
-
public static void main(String... args) {
-
int[] a = {4,5,6,7, 5, 6, 4};
-
int[]b = removeAll(a, 7, 4);
-
System.out.println(Arrays.toString(b));
-
}
-
public static int[] removeAll(int[] numbers, int length, int removeItem) {
-
Integer[] items = new Integer[numbers.length];
-
int removed = 0;
-
for(int i = 0; i < numbers.length;i++) {
-
if(numbers[i] == removeItem) {
-
items[i] = null;
-
removed++;
-
}
-
else {
-
items[i] = numbers[i];
-
}
-
}
-
numbers = new int[numbers.length - removed];
-
int index = 0;
-
for(int i = 0; i < items.length;i++) {
-
if(items[i] == null) {
-
}
-
else {
-
numbers[index++] = items[i];
-
}
-
}
-
return numbers;
-
}
-
}
Try it and post any error messages that you are getting with it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paul Rubin |
last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd
like to use an assignment expression, like
for line in file:
if (g := re.match(pat, line)):
croggle(g.group(1))
Since...
|
by: Andrew Koenig |
last post by:
It has been pointed out to me that various C++ books disagree about
the relative precedence of ?: and the assignment operators.
In order to satisfy myself about the matter once and for all,
I...
|
by: CoolPint |
last post by:
It seems to me that I cannot assign objects of a class which has a constant data
member since the data member cannot be changed once the constructor calls are
completed. Is this the way it is meant...
|
by: Edward Diener |
last post by:
Is there a way to override the default processing of the assignment operator
for one's own __value types ? I realize I can program my own Assign method,
and provide that for end-users of my class,...
|
by: Rick N. Backer |
last post by:
I have an abstract base class that has char* members. Is an
assignment operator necessary for this abstract base class? Why or
why not?
Thanks in advance.
Ken Wilson
Amer. Dlx. Tele,...
|
by: Graham |
last post by:
This has to do with class variables and instances variables.
Given the following:
<code>
class _class:
var = 0
#rest of the class
|
by: Kennedy_f |
last post by:
I did better in terms of score on this one than 291, but I found it much
harder. Wordings of questions are difficult like the rest, but the
DNS and CA scenarios were very tough to figure out. Take...
|
by: Neil Zanella |
last post by:
Hello,
I would like to know whether the following C fragment is legal in
standard C and behaves as intended under conforming implementations...
union foo {
char c;
double d;
};
|
by: nagy |
last post by:
I do the following. First create lists x,y,z. Then add an element to x
using the augumented assignment operator. This causes all the other
lists to be changed also.
But if I use the assignment...
|
by: TimeHorse |
last post by:
I would like to gauge interest in the following proposal:
Problem:
Assignment statements cannot be used as expressions.
Performing a list of mutually exclusive checks that require data...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |