Results 1 to 5 of 5

Thread: Converting a Collection to An Array

  1. #1
    Join Date
    Jan 2009
    Posts
    85

    Converting a Collection to An Array

    Hello, I am confused about the conversion while working in java. If anyone is having idea or any type of solution from which I can able to convert the collection to an array then please help me to achieve it. I will be thankful to you. So, please help me to achieve it.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Converting a Collection to An Array

    Hello, I have got the code below, while converting a collection to an array you will need to make use of the code below:
    Code:
    import  java.util.List;
    import java.util.ArrayList;
    public class CollectionToArray
    {
    public static void main(String[] args)
    {
    List lst = new ArrayList();
    lst.add("java");
    lst.add("program");
    lst.add("code");
    lst.add("variable");
    Object[] arr = lst.toArray();
    for (int i = 0; i < arr.length; i++)
    {
    System.out.println(arr[i].toString());
    }
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Converting a Collection to An Array

    Hello, you can simply make use of the code below for converting a collection to an array:
    Code:
    Object[] obj = list.toArray(); 
    MyClass[] arr = (MyClass[])list.toArray(new MyClass[list.size()]); 
    
    obj = set.toArray(); 
    arr = (MyClass[])set.toArray(new MyClass[set.size()]); 
    
    obj = map.keySet().toArray();
    arr = (MyClass[])map.keySet().toArray(new MyClass[set.size()]); 
    
    obj = map.values().toArray(); 
    arr = (MyClass[])map.values().toArray(new MyClass[set.size()]);

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Converting a Collection to An Array

    Hello, If you use the code below then you will able to get the conversion of the collection to an array. So, you will able to get the solution for your problem:
    Code:
    import java.util.ArrayList;
    import java.util.Collection;
    
    public class convert {
      public static void main(String argarrS[]) {
        String str[] = { "one", "two", "three", "four", "five",
            "six", "seven", "eight", "nice" };
        Collection collection = new ArrayList();
        for (int i = 0, n = str.length; i < n; i++) {
          collection.add(str[i]);
        }
        String arrS[] = (String[]) collection.toArray(new String[0]);
        for (int i = 0, n = s.length; i < n; i++) {
    
          System.out.println(arrS[i]);
        }
        collection.remove(str[3]);
        System.out.println(str[1] + " " + collection.contains(str[1]));
        System.out.println(str[3] + " " + collection.contains(str[3]));
      }
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Converting a Collection to An Array

    I am not having any knowledge for converting the collection to an array, but while searching on internet, I have got the following code, which will help you:
    Code:
    public coll GetWorkItemCollection(string str, int id, int num )
    {
          coll workItems = TaskAPI.SearchTasks(WorkItem.WorkItemStatus.OnHold, str, id, num);
    
          return workItems;
    }

Similar Threads

  1. How can I view my collection via tag?
    By Casula in forum Portable Devices
    Replies: 5
    Last Post: 10-07-2011, 07:20 PM
  2. Table or Map in Collection
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 12:41 AM
  3. How to convert array to collection in java?
    By Steadfast in forum Software Development
    Replies: 4
    Last Post: 06-02-2010, 04:22 PM
  4. Assigning an array to an array
    By MACE in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 05:19 PM
  5. Using collection in asp.net
    By Deepest BLUE in forum Software Development
    Replies: 3
    Last Post: 13-07-2009, 06:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,500,536.44047 seconds with 16 queries