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 5 of 5

Thread: Double Hashing for inserting integer into hashtable

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Double Hashing for inserting integer into hashtable

    I'm having a problem implementing double hashing when I insert a value into the table i get this for the output:
    Insert []. Is there something I'm doing wrong? I'm not getting any errors


               public static int size = 0;
    	   public static ArrayList<Integer>[] a;
     
    	       public static void interactiveMode(Scanner input) {
    	        int menuChoice = 0;
    	             do {
    	         System.out.println("\nInteractive Mode:");
    	         System.out.println("1. Enter size of table");
    	         System.out.println("2. Enter an integer");
    	         System.out.println("3. Find integer");
    	         System.out.println("4. Exit to Main Menu.");
    	         System.out.print("Please enter a selection: ");
     
    	         menuChoice = input.nextInt();
    	         input.nextLine();
    	         System.out.println();
    	         switch (menuChoice) {
    	            case 1:
    	               System.out.print("Enter size of table: ");
    	               int n = input.nextInt();
    	               size=n;
    	                a = new ArrayList[size];
    	               for (int i = 0; i < size; i++) {
    	                  a[i] = new ArrayList<Integer>();
    	               }
    	               print();
    	               break;
    	            case 2: 
    	            	System.out.println("Enter an integer for the table");
     
    	            	    int m = input.nextInt();
    						Hashtable ht=new Hashtable(size);
    	            	    for ( int i=0; i<a.length; i++ ) {
    	            	         System.out.println("\nInsert "+a[i]) ;
    	            	       insert( new ArrayList(a[i]));
    		               print();
    	            	    }
    		               break;
    	            case 3:
    	               System.out.print("Enter an integer to find: ");
    	               int search = input.nextInt();
    	               if (find(search)) {
    	                  System.out.println("Integer " + search + " is in the table.");
    	               }
    	               else {
    	                  System.out.println("Integer " + search + " is not in the table.");
    	               }
    	               break;
    	            default:
    	               if (menuChoice != 4) {
    	                  System.out.println("Enter correct number.");
    	               }
    	               break;
    	         }
    	      } while (menuChoice != 4);
    	   }
    Here's the methods:
     static int hash(Object key)
    	   {
    		return key.hashCode()%size;  
    	   }
    	   public static int hash2(Object key)
    	   {
    		   return  2*((key.hashCode())%7) + 1 ;
    	   }
    	   @SuppressWarnings("unchecked")
    	public static void insert(Object key)
    	   {
    		   int index = hash(key) ;
    	       int doublehash = hash2(key) ;
    	       while ( a[index] != null ) {
    	          index = (index+doublehash) % size ;
    	       }
    	       a[index] = (ArrayList<Integer>) key ;   
    	   }
    [/code]


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Double Hashing for inserting integer into hashtable

    You are not using ArrayLists correctly. The brackets '[' and ']' can only be used with the good old arrays, not with anything else.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Double Hashing for inserting integer into hashtable

    Oh okay I see, so how would I call my insert method?

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Double Hashing for inserting integer into hashtable

    Exactly the way the API specifies.
    ArrayList (Java Platform SE 6)

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Double Hashing for inserting integer into hashtable

    This requires careful thought as it's quite confusing. Did you deliberately create an array of ArrayLists? I'm asking because I see:
    public static ArrayList<Integer>[] a;  // 'a' is an array of ArrayLists
    ...
     
    // ... after entering the size of the table...
    a = new ArrayList[size];  // specify that the array has a size of [font=Courier New]size[/font]
    for (int i = 0; i < size; i++) {
        a[i] = new ArrayList<Integer>();  // instantiate and add ArrayList objects to array 'a'
    }
    An array of ArrayList is effectively a 2-dimensional data structure. Is this what you wanted?

    In the code for option #2 (Enter an integer),
    case 2: 
        System.out.println("Enter an integer for the table");
     
        int m = input.nextInt();
        Hashtable ht=new Hashtable(size);  // what is this for?
        for ( int i=0; i<a.length; i++ ) {
            System.out.println("\nInsert "+a[i]);
            insert( new ArrayList(a[i]));
            print();
        }
        break;
    The output that you got, "Insert []", is from System.out.println("\nInsert "+a[i]), where "[]" is from the empty ArrayList referenced by a[i]. In the meantime, you created a Hashtable object (ht). ht and the integer that is inputted by the user (m) are not used (at least not in the code that you posted). What's happening here?

    I'd suggest that you give further thought on what you want your code to do, and the data structure that you want to use. Pencil-and-paper might be the right approach for this.

    If you need help on using ArrayLists, you can take a look at a tutorial, e.g., Java Collections - List.

Similar Threads

  1. Confirming if a User Input is an Integer/Double
    By bgroenks96 in forum Java Theory & Questions
    Replies: 5
    Last Post: June 8th, 2011, 06:35 AM
  2. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM
  3. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  4. Typecasting of double variable to integer
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  5. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM