Home‎ > ‎Applied Core Java‎ > ‎Collection Framework‎ > ‎

TestArrays

Problem : Create a dynamic Arrays.

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestArrays.java  and copy below contents.

import java.util.Arrays;

import java.util.Iterator;
import java.util.List;

/*
 * A program to read all elements sequentially from a
 * List with help of Iterator interface
 */
public class TestArrays {

    public static void main(String[] args) {

        String[] stArray = new String[] {
                "Basanti-> Kahan  jana hai ",
                "Jay-> Ramgdh jana hai  ",
                "Basanti-> yuki Basanti ko jada bate karne ki aadat to hai nehai..",
                "Basanti-> isaliye  2 rupay lagaga ",
                "Viru-> Tuhara  nam kya hai basanti "
        };
       
        // Convert an Array into List collection
        List list = Arrays.asList(stArray);
       
        // Get an iterator from List
        Iterator it = list.iterator();
       
        System.out.println("From Iterator ");
       
        //Check if Iterator has next element
        while (it.hasNext()) {
            //Get next element
            System.out.println(it.next());
        }
    }

}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestArrays.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestArrays.java with help of 
    • javac TestArrays.java command. Compiled class file will be created in the same folder
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java TestArrays command.

Output

From Iterator
Basanti-> Kahan  jana hai
Jay-> Ramgdh jana hai
Basanti-> yuki Basanti ko jada bate karne ki aadat to hai nehai..
Basanti-> isaliye  2 rupay lagaga
Viru-> Tuhara  nam kya hai basanti

FAQ

What is Arrays Class?

  1. This class contains various methods for manipulating arrays such as sorting and searching.
  2. This class also contains a static factory that allows arrays to be viewed as lists.
  3. The methods in this class all throw a NullPointerException if the specified array reference is null.


<<Previous | Next>>
ċ
TestArrays.java
(1k)
Mahima Hardia,
Aug 20, 2011, 6:37 AM
Comments