473,503 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble with arraylists

3 New Member
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);
}
}
}
Sep 1 '07 #1
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).
Sep 1 '07 #2
coolieman
3 New Member
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 ]

Expand|Select|Wrap|Line Numbers
  1. for (int ind = 0; ind<nbrs.size(); ind++) {
  2.             if (nbrs.get(ind) %2 == 0) {
  3.                 nbrs.add(ind+1,nbrs.get(ind));
  4.             }
  5.         }
  6.         System.out.println("nbrs = "+nbrs);
  7.  
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.
Sep 2 '07 #3
kreagan
153 New Member
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 ]

Expand|Select|Wrap|Line Numbers
  1. for (int ind = 0; ind<nbrs.size(); ind++) {
  2.             if (nbrs.get(ind) %2 == 0) {
  3.                 nbrs.add(ind+1,nbrs.get(ind));
  4.             }
  5.         }
  6.         System.out.println("nbrs = "+nbrs);
  7.  
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?
Sep 2 '07 #4
coolieman
3 New Member
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?
Sep 2 '07 #5
Ganon11
3,652 Recognized Expert Specialist
Perhaps a debugger would be catching these problems before they compiled...
Sep 2 '07 #6
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:

Expand|Select|Wrap|Line Numbers
  1. for (int i= 0, i < list.length(); i++)
  2.    if (removePredicate(list.get(i)) 
  3.       list.remove(i--);
  4.  
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:

Expand|Select|Wrap|Line Numbers
  1. private boolen removePredicate(int i) {
  2.    int d= i%10;
  3.    return d+1 == 8 || d == 8 || d-1 == 8;
  4. }
  5.  
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
Sep 2 '07 #7
madhoriya22
252 Contributor
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.
Sep 3 '07 #8
sateesht
41 New Member
Hi,

Below is the Code :

<spoonfeeding code removed; it was bad code as well>

Cheers,
Sateesh.
Sep 6 '07 #9
Nepomuk
3,112 Recognized Expert Specialist
Hi,

Below is the Code :
Expand|Select|Wrap|Line Numbers
  1. <spoonfeeding code removed; it was bad code as well>
  2.  
Cheers,
Sateesh.
Is there any specific reason, why you have a List of Strings, when you're working with numbers?

Greetings,
Nepomuk
Sep 6 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1082
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. ...
0
1188
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...
1
2588
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
5
2384
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 ...
0
1223
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...
3
3184
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...
4
1675
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...
2
3994
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();...
1
1758
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...
0
7087
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...
0
7334
jinu1996
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...
0
7462
tracyyun
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...
0
4675
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...
0
3168
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...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
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 ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.