Question

Deleting from arraylist

Asked by: jaggernat

hi guys

List editionsToAdd = new ArrayList();
            for (int i = 0; i < listFromFiles.size(); i++)
            {
                  editionsToAdd.add(listFromFiles.get(i));
                        
            }

String editiontoCreate = (String) itemIdandEdtionIdinFTP.get(p);
      if (itemIdandEditionIdinDB.contains(editiontoCreate))  
                  {
                        editionsToAdd.remove(editiontoCreate);
                        System.out.println("Names in editionsToAdd"+editionsToAdd.size());
                  }

//editiontoCreate   contains abc,def,xyz,eee
// itemIdandEditionIdinDB contains abc,def,xyz
//editionsToAdd also contains  abc,def,xyz,eee

the above if condition is getting satisfied, but editionsToAdd.size()  is not decreasing
for instance , the first time when itemIdandEditionIdinDB and editiontoCreate  are both abc,
i am removing abc from editionsToAdd, but still System.out.println("Names in editionsToAdd"+editionsToAdd.size());
prints 4 .

any ideas?
thanks
J

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-10-10 at 10:56:22ID22019474
Tags

arraylist

,

deleting

,

remove

,

from

Topic

Java Programming Language

Participating Experts
3
Points
500
Comments
24

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Referencing Problem in Java (for arraylists)
    I am facing a referencing problem. I have a program that uses 2 arraylists, namely, "neighbors" And "allNodes" They get initialized in my program by accessing a static variable "a" of another program. For some strange reason, any modifications...
  2. ArrayList of Objects Question
    Can anyone show me a simple example using Visual Basic .Net of how to use an arraylist of objects to: add to the arraylist, edit an item in the arraylist, delete an item in the arraylist, sort the arraylist and search the arraylist? For example, if I had an arraylist contain...
  3. ArrayList
    How can I replace an item in a listbox by some other text? Also, to display wheter or not an item is found? Dim arrList As ArrayList = New ArrayList #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call...
  4. Arraylist
    I need to know how to add and delete items in an arraylist specified by the index.
  5. Arraylist - delete
    Is it possible to delete any values from Datagrid? Public Class Names Private mName As String Public Sub New(ByVal nm As String) mName = nm End Sub Public Property Name() As String Get Return mName End Get Set(...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: CEHJPosted on 2006-10-10 at 10:59:00ID: 17700772

>>editionsToAdd.add(listFromFiles.get(i));

is in a loop. Nothing else is

 

by: valipotorPosted on 2006-10-10 at 11:09:42ID: 17700859

Maybe the editiontoCreate string is not in the array. An extra space can go unnoticed and cause problems, especially if dealing with text files.

 

by: aflat362Posted on 2006-10-10 at 11:09:52ID: 17700862

add some debug - check if editionstoAdd contains the string you're trying to remove
I bet the strings are different.

Sorry about the sloppy indenting.

     if (itemIdandEditionIdinDB.contains(editiontoCreate))  
               {

  if(editionsToAdd.contains(editiontoCreate)){
System.out.println("editionstoAdd contains " + editiontoCreate + " REMOVE" );
                    editionsToAdd.remove(editiontoCreate);
}else{
System.out.println("editionstoAdd doesn't contain " + editiontoCreate + " so don't REMOVE" );

}



                    System.out.println("Names in editionsToAdd"+editionsToAdd.size());
               }


 

by: CEHJPosted on 2006-10-10 at 11:12:46ID: 17700895

>>Maybe the editiontoCreate string is not in the array

Even if it is, it's editionsToAdd.size() that's being called

 

by: jaggernatPosted on 2006-10-10 at 11:17:35ID: 17700941

ok, i put a for loop for the remove statement , still no luck

if (itemIdandEditionIdinDB.contains(editiontoCreate))  
                  {
                        for(int h=0;h<editionsToAdd.size();h++)
                        {
            //Remove the editions with same ItemId and EditionId so that editionsToAdd contains only the brand new report editions
                              editionsToAdd.remove(editiontoCreate);
                              System.out.println("Names in editionsToAdd"+editionsToAdd.size());      
                        }
                        
                  }

 

by: aflat362Posted on 2006-10-10 at 11:18:03ID: 17700943

not sure where you're going with this CEHJ

He's doing this:

editionsToAdd.remove(editiontoCreate);
System.out.println("Names in editionsToAdd"+editionsToAdd.size());


He's trying to remove an object from the ArrayList and then check its size.
He's expecting an object to be removed and its not (most likely 'cause the string isn't in the arraylist)

Nothing to do with what's in a loop or editionsToAdd.size() being called

Maybe I'm missing something . . .

 

by: CEHJPosted on 2006-10-10 at 11:20:27ID: 17700959

>>Maybe I'm missing something . . .

Sorry - it's me (losing concentration)

 

by: aflat362Posted on 2006-10-10 at 11:20:34ID: 17700961

jaggernat - that last bit of code is confusing.

You are looping once for each item in the "edidtionsToAdd" ArrayList
In each time the loop runs you try to remove the same thing from the ArrayList.

 

by: CEHJPosted on 2006-10-10 at 11:22:01ID: 17700975

>>
You are looping once for each item in the "edidtionsToAdd" ArrayList
In each time the loop runs you try to remove the same thing from the ArrayList.
>>

That's why i drew attention to the loop above

 

by: aflat362Posted on 2006-10-10 at 11:23:42ID: 17700989

Ok,

Jaggernat - how about you explain in english exactly what you're trying to achieve with the loops and Lists and either CEHJ or myself will produce the code.

Sound good?

 

by: jaggernatPosted on 2006-10-10 at 11:24:48ID: 17700996

sorry guys  , my bad.
actually  editionsToAdd  contains something like
RIINF0010-8092-03-06-1.PDF
and
editiontoCreate  contains something like
RIINF0010-8092-03-06

that is why i think
editionsToAdd.remove(editiontoCreate);
 is not able to remove.

what say you?

 

by: aflat362Posted on 2006-10-10 at 11:25:30ID: 17701001

Sounds like a good reason to me for it not to work

 

by: jaggernatPosted on 2006-10-10 at 11:26:48ID: 17701012

so now the problem is i want to remove RIINF0010-8092-03-06-1.PDF from editionsToAdd  if
editiontoCreate is RIINF0010-8092-03-06?

ideas?

thanks

 

by: aflat362Posted on 2006-10-10 at 11:27:48ID: 17701022

would something as simple as this work for you?

editionsToAdd.remove(editiontoCreate + "-1.PDF");

 

by: CEHJPosted on 2006-10-10 at 11:28:37ID: 17701029

>>so now the problem is i want to remove RIINF0010-8092-03-06-1.PDF from editionsToAdd  if

You'll have to iterate and substring them

 

by: aflat362Posted on 2006-10-10 at 11:31:49ID: 17701061

iterating and substringing - not checked for syntax or anything.

most likely "mySearchString" will be populated for you in your parent loop.


String mySearchString = "RIINF0010-8092-03-06";

for(int i=0; i < someArrayList.size(); i++){
      String currentString = someArrayList.get(i);
      //if the search string is found
      if (currentString.subString(mySearchString) != -1){
            someArrayList.remove(i);
      }
}



String mySearchString = "RIINF0010-8092-03-06";

for(int i=0; i < someArrayList.size(); i++){
      String currentString = someArrayList.get(i);
      //if the search string is found
      if (currentString.subString(mySearchString) != -1){
            someArrayList.remove(i);
      }
}

 

by: CEHJPosted on 2006-10-10 at 11:33:37ID: 17701074

Since your logic depends on it, you should encapsulated the 'fields' of that String in a class, or you'll always be hacking at it and perforce producing less than elegant code

 

by: jaggernatPosted on 2006-10-10 at 11:49:24ID: 17701203


i dont think it will work aflat, its not just one string. Let me explain with some sample data.



editionsToAdd contains
RIINF0010-8092-03-06-1.PDF
RIINF0010-8092-03-06-2.PDF
RIINF0010-8092-03-06-3.PDF
RIINF0010-7062-03-06-1.PDF
RIINF0010-7062-03-06-2.PDF
RIINF0010-7062-03-06-3.PDF
RIINF0010-6062-03-06-3.PDF
..
editionsToAdd  is generated dynamically

editiontoCreate (also generated dynamically) contains
RIINF0010-8092-03-06
RIINF0010-7062-03-06
..more

Now since "part of editionsToAdd" and "editiontoCreate"  is exactly the same , i want to delete all common entries from editionsToAdd
so i want to delete
RIINF0010-8092-03-06-1.PDF
RIINF0010-8092-03-06-2.PDF
RIINF0010-8092-03-06-3.PDF
RIINF0010-7062-03-06-1.PDF
RIINF0010-7062-03-06-2.PDF
RIINF0010-7062-03-06-3.PDF

from editionsToAdd so that editionsToAdd   is just left with RIINF0010-6062-03-06-3.PDF ,(the uncommon string)

The above data is just sample, editionsToAdd would actually contain lots and lots of names.

any ideas greatly appreciated
thanks
J

 

by: CEHJPosted on 2006-10-10 at 11:58:24ID: 17701268

It won't work. You need to substring if you don't follow my last suggestion

 

by: jaggernatPosted on 2006-10-10 at 12:09:13ID: 17701342

>>You need to substring

can you tell me what i have to substring?

thanks
J

 

by: aflat362Posted on 2006-10-10 at 12:10:54ID: 17701353

look at the code example I posted @ 10/10/2006 01:31PM CDT  - this is what cehj was talking about.

 

by: CEHJPosted on 2006-10-10 at 12:11:03ID: 17701354

You already know - you've been doing it in your other q ;-)

 

by: jaggernatPosted on 2006-10-10 at 13:18:33ID: 17701933

used substring and got it running.

thank you cehj and aflat.

J

 

by: CEHJPosted on 2006-10-10 at 15:25:32ID: 17702895

:-)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...