Hi, Everyone, recently my programming class has started making programs involving arraylists. Our program is supposed to delete all numbers in the arraylist that contain numbers that end in the number we input and also numbers that are either +1 or -1 from the number.
Ex.
arrayList = [43, 59, 87, 60, 67, 25]
What number do you wish to input?
8
arrayList = [43, 60, 25]
^ This is the output that should occur. Removes all numbers that end in 8, +1(9) and -1(7) of it.
However, this is the real result I get. Ex.
arrayList = [43, 59, 87, 60, 67, 25]
What number do you wish to input?
8
arrayList = [43, 87, 60, 25]
The 87 remains even though it should be gone. Does anyone know what the problem is? Here is the for loop I used to check from numbers that were equal to the number, +1, and -1 of that number.
for (int ind = 0; ind<nbrs.size(); ind++) {
if (nbr > 0 || nbr < 9) {
if ((nbrs.get(ind) %10) == (nbr) || (nbrs.get(ind) %10) == (nbr+1) || (nbrs.get(ind) %10) == (nbr-1)) {
nbrs.remove(ind);
}
}
else if (nbr == 9) {
if ((nbrs.get(ind) %10) == nbr || (nbrs.get(ind) %10) == (nbr-1)) {
nbrs.remove(ind);
}
}
else if (nbr == 0) {
if ((nbrs.get(ind) %10) == nbr || (nbrs.get(ind) %10) == (nbr+1)) {
nbrs.remove(ind);
}
}
}
9 1311 Ganon11 3,652
Recognized Expert Specialist
When you remove an item, you aren't doing anything to change the index. Suppose you have two elements in a row to remove (let's say at indexes 3 and 4). When your loop gets to array[3], it removes the item as is expected. But now the value that was in array[4] has moved to array[3], so you should check that spot again, yes? But, as your code stands, your loop will continue with checking the element in array[4].
Every time you remove an item, you should set the index back 1 again, so that it will check the same spot again (which is occupied by a new value).
Thanks for the tip Ganon. I added an "ind--" into all 3 of the if statements and did the trick.
Now lets say that I want to make the program duplicate all values that are even.
Ex.
arraylist = [ 24, 25, 44, 22, 12 ]
would be turned into
arraylist = [ 24, 24, 25, 44, 44, 22, 22, 12, 12 ] - for (int ind = 0; ind<nbrs.size(); ind++) {
-
if (nbrs.get(ind) %2 == 0) {
-
nbrs.add(ind+1,nbrs.get(ind));
-
}
-
}
-
System.out.println("nbrs = "+nbrs);
-
Although the program compiles fine, I get an error message stating "OutOfMemoryError: Java heap space (in java.util.Arrays)" when I try to run. Do you have any suggestions on how to work around this?
Thanks
EDIT: Got it fixed. The problem was that it went in an endless loop. Adding an ind++ in the if statement fixed the problem.
Thanks for the tip Ganon. I added an "ind--" into all 3 of the if statements and did the trick.
Now lets say that I want to make the program duplicate all values that are even.
Ex.
arraylist = [ 24, 25, 44, 22, 12 ]
would be turned into
arraylist = [ 24, 24, 25, 44, 44, 22, 22, 12, 12 ] - for (int ind = 0; ind<nbrs.size(); ind++) {
-
if (nbrs.get(ind) %2 == 0) {
-
nbrs.add(ind+1,nbrs.get(ind));
-
}
-
}
-
System.out.println("nbrs = "+nbrs);
-
Although the program compiles fine, I get an error message stating "OutOfMemoryError: Java heap space (in java.util.Arrays)" when I try to run. Do you have any suggestions on how to work around this?
Thanks
EDIT: Got it fixed. The problem was that it went in an endless loop. Adding an ind++ in the if statement fixed the problem.
lol.
Just wondering, do you use a debugger?
lol.
Just wondering, do you use a debugger?
No. I'm just a student and I use BlueJ as my compiler. Why do you ask though?
Ganon11 3,652
Recognized Expert Specialist
Perhaps a debugger would be catching these problems before they compiled...
JosAH 11,448
Recognized Expert MVP
I'm jumping in a bit late but nevertheless: I hate all those complicated if-clauses
inside the body of a loop; IMHO, the loop should look like this: -
for (int i= 0, i < list.length(); i++)
-
if (removePredicate(list.get(i))
-
list.remove(i--);
-
The navigation is performed in the loop; when some 'predicate' tells the element
should be removed, it is removed and the index is adjusted accordingly.
The predicate itself looks something like this: -
private boolen removePredicate(int i) {
-
int d= i%10;
-
return d+1 == 8 || d == 8 || d-1 == 8;
-
}
-
This clearly separates the 'business logic' from the 'mechanics'. The predicate
method takes care of the first, the loop takes care of the mechanics.
If people are too afraid to build an additional class or method they end up with
complicated control flow, funny data structures and Swiss army knife methods
and classes. For some well defined singular functionality never hesitate to
create a separate method for it. For some well defined entity, never hesitate to
create a new class for it.
kind regards,
Jos
For some well defined singular functionality never hesitate to
create a separate method for it. For some well defined entity, never hesitate to create a new class for it.
kind regards,
Jos
Hi Jos,
Nice tip :). I am noting down all ur tips and suggestions for future help.
Hi,
Below is the Code :
<spoonfeeding code removed; it was bad code as well>
Cheers,
Sateesh.
Nepomuk 3,112
Recognized Expert Specialist
Hi,
Below is the Code : -
<spoonfeeding code removed; it was bad code as well>
-
Cheers,
Sateesh.
Is there any specific reason, why you have a List of Strings, when you're working with numbers?
Greetings,
Nepomuk
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Brandon Potter |
last post by:
Trying to find the best way to find common entries in an x number of
ArrayLists or arrays of integers. Curious if there is a method already
available in .NET to do just this very thing.
...
|
by: Meta-Meta |
last post by:
Hi,
I'm trying to use XmlSerialization (C++ .NET) for ArrayLists and am getting
frustrated! I'd appreaciate any explanations (the ones I found on google so
far didn't do it for me).
Here's my...
|
by: godsella |
last post by:
First i have two stored procedures, i have passed the values of each
one into two different arraylists of dates. how can i compare the two
arraylists of dates?
Thanks in advance
|
by: drdave |
last post by:
I would like to have ten arraylists created within a loop.. is there a
conversion or something I can do to acheive this..
pseudo:
Dim counter As Integer = 0
Dim ArrName As ArrayList
...
|
by: steve |
last post by:
I'm looking for a code example how to compare the values in a given record in different arraylists
two arraylists, two fields in each record, both defined as string
I'm thinking that it's...
| |
by: steve |
last post by:
I need to compare the value of a field in a row on an arraylist with the value of a field on a second arraylist
I have this bit of code working for arrays but cant get it working for arraylists
The...
|
by: Andy in S. Jersey |
last post by:
I would like to create an unknown number of Arraylists, meaning, I don't know
how many I should create until runtime.
I will be reading a table, and 0,1,2, or more fields have to be put into...
|
by: Andy in S. Jersey |
last post by:
I would like to create an unknown number of ArrayLists, that is I don't know
that until runtime.
Of course I can do this if I knew I needed two:
ArrayList IntervalArray1 = new ArrayList();...
|
by: Newbie19 |
last post by:
I'm just learning java arrays/arraylists and was wondering what are the best books for learning java arrays/arraylists?
I know practice is the best way to learn, but I have a hard time...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |