Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Threaded View

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Post Generic programming example

    I hope to be useful for everyone.

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.List;
    import java.util.Map;
     
    //Note : Java can't insert primitives into Tables,Lists or anything else expecting an Object
    // So java provides wrapper types for this purpose (int ---> Integer).
    public class GenericDemo {
     
        /** Creates a new instance of GenericDemo */
        public GenericDemo() {
        }
     
        public static void main(String args[]){
     
            /*
             *Here i'm create a arraylist hold integers only
             *Simple if you want create generic type
             *write_Container_Name <TypeName> varName = new write_Container_Name <TypeName>();
             *varName.Member Function and so on.
             */
            List<Integer> iArrayList = new ArrayList<Integer>();
            iArrayList.add(100);
            iArrayList.add(500);
            //View Container content.
            for(int i = 0 ; i < iArrayList.size();i++)
            {
                System.out.println(iArrayList.get(i));
            }
     
            /*
             *Here i'm create a Maplist hold string for every item.
             */
            Map<String,String> mplist = new HashMap<String,String>();
            mplist.put("0","Author : Mohamed Saleh");
            mplist.put("1","Subject : Demonstration about Generic Programming");
            mplist.put("2","Audience : Every Programmer what to know how Generic Programming Looks Like.");
            mplist.put("3" , "Subject : Generic Programming\nThis is (Genetics):Using DataStructure Object Depend On my Own Like (Any Object)\n");
            mplist.put("4","Sample without Generic : //Declare a ArrayList\n ArrayList al = new ArrayList();\n al.add(\"Object\");\n al.add(500);");
            mplist.put("5","Hint 01//This ArrayList al will manipulate with any object Type\nWhat if i want to manipulate with strings Only for specific purpose.\nSo i have to declate special type ArrayList for string in Same Time.");
            mplist.put("6","Sample with Generic// ArrayList<String> myStrList = new ArrayList<String>()");
            mplist.put("7","Hint 02//Then Using Current new Object Directly");
     
            for(int x = 0 ; x < mplist.size() ; x++){
                  // Here i want to view data holds in MapList and there is a restriction (View By Key not Index Like Arrays or ArrayList)
                  // so i did a convert or casting to content of Key to Object
                  System.out.println(mplist.get((Object)Integer.toString(x)));
            }
     
            /*
             *Here i'm create a Hashtable holds doubles only
             */ 
            Hashtable<Double,Double> dblTable = new Hashtable<Double,Double>();
     
        }
    }

  2. The Following 2 Users Say Thank You to neo_2010 For This Useful Post:

    JavaPF (July 8th, 2009), Json (July 8th, 2009)