Results 1 to 5 of 5

Thread: How to convert array to collection in java?

  1. #1
    Join Date
    Dec 2009
    Posts
    42

    How to convert array to collection in java?

    Hello to all,
    I am new to this forum. I am last year Computer Science student. I recently started learning java language. In our last lecture our sir asked one question how to convert array to collection in java? No one is able to give answer, that's why I am asking this question on this forum. Can anyone able to give answer of this question with proper example.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to convert array to collection in java?

    Hey it is very easy process to convert array to collection in java. You have to just use java.util.Arrays class to do this. We are bale to converts array into collection using static method asList(Object[] a) which is present in java.util.Arrays class. Just try to understand it. It is very simple. In this program I have use java.util.List class to convert array to collection.



    Code:
    import java.util.Arrays;
    import java.util.List;
    import java.util.Iterator;
    
    public class ArraystocolleEg
    {
        public static void main(String[] args)
        {
            String[] array1 = {"Welcome", "to", "this", "world"};
            List list1 = Arrays.asList(array1);
    
            Iterator iterators = list1.iterator();
            while (iterators.hasNext())
            {
                System.out.println((String) iterators.next());
            }
        }
    }

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

    Re: How to convert array to collection in java?

    It is very simple to convert array to collection in java. In have written follwing code for you to convert array to collection in java. You have to just use this code in your program for converting array to collection. In the following program I have create one array variable to store user value and after that I have pass this variable to Arrays.asList() method for converting array to collection.



    Code:
    Object[] arrays1 = new Object[]{"98","32","54"};
    java.util.List lists = Arrays.asList(arrays1);

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

    Re: How to convert array to collection in java?

    I have written following program in java to convert array to collection. It is very simple program. You have to just use java.util. to do this. You can do this by using "import java.util.*;" value. In the following program I also have use java.io.* class to import input and output methods.


    Code:
    import java.util.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Collection;
    
    public class CollToArrEG {
      public static void main(String args[]) {
        String allweekdays[] = { "Sundays", "Mondays", "Tuesdays", "Wednesdays",
     "Thursdays","Fridays", "Saturdays"};
        Collection<String> dayss = new ArrayLists<String>();
      for (int k = 0, p = weekdays.length; k < p; k++) {
        days.add(weekdays[k]);
        }
        String sp[] = (String[])dayss.toArray(new String[0]);
        for (int k = 0, p = sp.length; k < p; k++) {
          System.out.println(s[k]);
          }
          }
    }

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

    Re: How to convert array to collection in java?

    You can convert array to collection using following java program. It is very simple program. In the following program I have use java.util.Arrays class to accept array variable and I have also use java.util.Collections class to convert array to collection. In the following program I have create one class known as "arrSort" and using this class I have create one object of it for storing all array variable.



    Code:
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    public class arrSort {
      public static void main(String args[]) {
        String[] strArrays = new String[] { "Java", "is", "very", "simple",
            "and", "easy" , "language" };
    
        List k = Arrays.asList(strArrays);
        Collections.arrsort(k);
        System.out.println(k);
      }
    }

Similar Threads

  1. How to convert DVD collection on PC?
    By Tufan in forum Windows Software
    Replies: 4
    Last Post: 28-07-2011, 08:49 AM
  2. JAVA - What is garbage collection
    By Bhim in forum Software Development
    Replies: 5
    Last Post: 28-04-2011, 01:13 PM
  3. How to convert string to char array in java?
    By Baazigar in forum Software Development
    Replies: 6
    Last Post: 10-01-2011, 06:36 PM
  4. Java - help with Garbage Collection
    By Messenger in forum Software Development
    Replies: 4
    Last Post: 23-07-2010, 10:18 AM
  5. Converting a Collection to An Array
    By Joyjeet in forum Software Development
    Replies: 4
    Last Post: 16-02-2010, 08:48 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,751,804,458.36787 seconds with 16 queries