• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem ArrayList

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

i have an ArrayList where in there is a blank objects in between the String Objects in ArrayList. i Want to know how to get rid of this when i copy to another ArrayList.

The Code chunk which i tried is as follows
---------------------------------------------------------------------------
ArrayList l;
for(int z=0;z<indatQ.size();z++)
{
if(null!=indatQ.get(z) && indatQ.get(z)!="")
{
l.add(String.valueOf(indatQ.get(z)));

}
}

---------------------------------------------------------------------------
The out put is as follows when Queried by another Array ie Z.

value is : who is the founder of Microsoft
value is
value is : whos the fournder of java
value is :
value is whos the founder of Webservices
value is :
value is : which of thes is the indian Company
value is :

Please let me know how to get rid of this when i copy to another arrayList.


Thank you
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ranganath .S.Junpal:

The first part of this IF statement is not working, because it seems your blank ArrayList items are not truly null, they are simply blank strings. I see that you are trying to catch blank strings with the second part, but remember, that since String is not a primitive type, you cannot use != with it, you must use String.equals().

That IF statement should be changed to:Now, we're casting the object to a String, then using the equals() method to really check if the object is a blank string.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The cast to String is unnecessary, as equals() is a method inherited from Object, but the point about the "!=" operator is correct. Your call to String.valueOf() is also unnecessary: if the object is a String, then it's a String.

It seems as though you ought to rearrange this loop anyway, as you're calling "get" three times:

 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply, use the removeAll method from the Collection interface implemented by all lists.


list.removeAll(Arrays.asList(null,"")); //remove unwanted items




I hope it helps!
[ January 15, 2006: Message edited by: Edwin Dalorzo ]
 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to all.. it really helped me a lot.. thanks a lot...


Ranganath.S
 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,
Actually i did try out the different suggestions which you guys mentioned in your post.. But it really didnt solve my Problem. still i am not able to solve it.

The code looks as follows:-
---------------------------------------------------------------------------
public class Xfilter
{
public void refineQuestion(ArrayList indatQ)
{
int counter=0;
// this just a class which has get/set accessors
XQuestionVO rdat=new XQuestionVO();

for(int z=0;z<indatQ.size();z++)
{
Object obj=indatQ.get(z);
if(null != obj && !"".equals(obj))
{
rdat.setQuestion(String.valueOf(indatQ.get(z)));
/* This will just add the string from the indatQ ArrayList to the Set Accessor, even in this Accessor i have an arraylist to which the //text is suppose to get updated. */
}
}
ArrayList yo=rdat.getQuestion();
for(int l=0;l<yo.size();l++)
{
System.out.println(" value is"+yo.get(l));
}
}
}

---------------------------------------------------------------------------
Output
===========================================================================
value is : whos the fournder of java
value is :

value is : whos the founder of blahblah
value is :

value is :some text
value is :

---------------------------------------------------------------------------
i did tried to use that removeAll() but then i using the ArrayList which is sent as a parameter.. so i wasnt sure how to use it..
please let me know if there is any other way through which i can solve this.. my project is on hold..

Thank you
Regards
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i did tried to use that removeAll() but then i using the ArrayList which is sent as a parameter.. so i wasnt sure how to use it..



Ranganath, comrade, what is the problem with the remove all?, whether it is a parameter or not, in Java objects are passed by reference. So, what is the problem?, we cannot help you seek to find new alternatives if you say you are not sure about the ones you already have.

Come on pal, let's see what's worng with the options you have. Tell us about it. And please, I beg you post code that we can execute, or at least emphisize where the problem is with comments. I feel your previous example is not so obivous about what is wrong.
[ January 16, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Give a try with a trim() to your String before the equals. Just check it isnt null before the trim().
 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude Edwin,

I tried using the way which u suggested.. My eclipse3.1(JDK1.5_03) says that
"The type List is not generic; it cannot be parameterized with arguments <String>" and it also says like this "Syntax error, parameterized types are only available if source level is 5.0 "


I just tried to declare like this..

List<String> ob=ArrayList<String>();

it is having problem.. please let know if you have any idea about this.
Hope my Question is Clear...

Thank you
Regards
reply
    Bookmark Topic Watch Topic
  • New Topic