Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2007, 07:55 AM
Registered User
 
Join Date: May 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Collections_toArray(object[])


hi,

i have created application to add string values and Integer Values in ArrayList. but I dont know how to extract String values only from the ArrayList. Please anyone say how to deal with it?.

Thank you

 
Old July 27th, 2007, 12:50 AM
Authorized User
 
Join Date: Oct 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
If I understand you right, you need to separate only String values from your array list.

I can suggest you the following way:

As all the ArrayList elements are Objects you can use toString() method fot them and get Strings for all of your ArrayList elements, even for Integers. Then you can simply check whether current arraylist_element.toString() characters are entirely 'numeric' or not, e.g. character is between '0' and '9'. If all the characters are 'numeric' then you have a integer number and you can remove it from your ArrayList.

Hope this helps you :)

Best,
Anna
 
Old August 8th, 2007, 07:31 AM
Registered User
 
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to pvc_27
Default

Another way would be creating a new ArrayList. Use either the 'instanceof' operator or the getClass() method.

You can loop through the ArrayList, check if the current element is an instanceof 'String' OR if currElement.getClass() == String.class, and if it is, you add it to a new ArrayList.

HTH!
 
Old September 14th, 2007, 08:38 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Hi,
  Check this program. I created two new Arraylists,one will contain integer values and second will contain String values.

   public static void main(String argds[]){

        ArrayList aList = new ArrayList();
        aList.add("Java");
        aList.add("123");
        aList.add("Sun");
        aList.add(new Integer(5));
        aList.add("J2EE");

        ArrayList intList = new ArrayList();
        ArrayList strList = new ArrayList();

        for(Object o:aList){
            try{
                Double.parseDouble(o.toString());
                intList.add(o);
            }
            catch(Exception e){
                strList.add(o);
            }
        }
        for(Object o:intList){
            System.out.println(o);
        }
        for(Object o:strList){
            System.out.println(o);
        }
    }

I hope this can be useful.

Venkat.K





Similar Threads
Thread Thread Starter Forum Replies Last Post
object reference not set to instant of an object shahidrasul ASP.NET 2.0 Basics 1 September 5th, 2008 02:01 PM
Serializing Object Graph - Assigning Object to Jag venkat.kl C# 0 August 28th, 2006 10:39 AM
create a Line object ,Box object in CR at Runtime? thanhnt Pro VB 6 1 May 16th, 2005 06:51 AM
Error Occurred creating Report Object: Object does sa_moizatyahoo Classic ASP Professional 0 February 1st, 2005 10:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.