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 1319 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 "OutOfMemoryErr or: Java heap space (in java.util.Array s)" 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 "OutOfMemoryErr or: Java heap space (in java.util.Array s)" 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.
Situation is that each array contains search results for a given criteria,
but the end result should only give the search results that are common to
all arrays of results.
i.e.,
SearchArrayOne = 100
|
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 example :
public __gc class ServerConfig
public:
ServerConfig(void)
|
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
'******** LOOP OVER THE VALUES ********************
|
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 something like
If ((fieldname)array1(x)) == ((fieldname)array2(x)) Then do something, where x is an integer used for the index/record or possibly
| |
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 secone argument here (1) represents the second field in the row, with arraylists I get a message saying to many arguments.
Can I do this with arraylists or do I need to copy the arraylists to arrays?
Thanks
For q As Integer = 0 To 9
For j...
|
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
individual ArrayLists.
How do I create an unknown number of ArrayLists?
Right now I put all the values into one ArrayList. I know how many fields
are in the ArrayList so I then know that every one, or every other one, or
|
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();
ArrayList IntervalArray2 = new ArrayList();
but what if I don't know how many I need at build time?
|
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 remembering the little items on arrays/arraylists.
Thanks,
Newbie
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |