Link to home
Start Free TrialLog in
Avatar of the_b1ackfox
the_b1ackfoxFlag for United States of America

asked on

Java array blues My array doesn't initialize

This is driving me nuts.  Why won't the following code initialize my array?

import java.util.*;

class HashTable
{
        static int ccount;
       
        static String str;
        //static String str2;
        static int hash_table[] = new int[64];
        static Bucket Buckets[] = new Bucket[64];

public static void main(String [] args)
{
      int j;
        int index = 0;
      int free_hash = 64;

      System.out.println( "64 random strings..." );
      System.out.println();

      for(j = 0; j < 64; j++)
      {
            
                make_string();
                index = hash(str);
            if ( 0 == hash_table[index])
                  free_hash--;
            hash_table[index]++;
            String tmpstr=str;
            
            Buckets[index].Add(tmpstr);
      }
      
      System.out.println();
      System.out.println();
      System.out.println( "Hash Table..." );
      System.out.println();

      for(j = 0; j < 64; j++)
      {
            System.out.print( hash_table[j] + "\t" );
            if(7 == j % 8)
                  System.out.println();
      }
      System.out.println();
        System.out.println( "The number of free locations is " + free_hash );
}

static int hash(String name)
{
      int middle, hash_value;
            
        middle = name.length() / 2;
        hash_value = name.charAt(0) + name.charAt(middle) + name.charAt(name.length() - 1);
        hash_value %= 64;
      System.out.print( "hash = " + hash_value + "\t");
      if (++ccount == 3)
      {
            ccount = 0;
            System.out.println();
      }
      return hash_value;
}
static void make_string()
{
      int i,k;

      Random genran = new Random();

      do
      {
            k = genran.nextInt(6);
        } while(k == 0);
        str = "";
        for(i = 0; i < k; i++)
        {
                str += (char)(65 + genran.nextInt(25));
        }
        System.out.print( str + "\t" );
}

public class DCard{
      DCard NextCard;
      String Text;
      DCard(){
            NextCard=null;
            Text="";
      }      
      DCard(String txt){
            Text=txt;
            NextCard=null;
      }
      public boolean EndofLine(){
            boolean results=false;
            if(NextCard==null){
                  results=true;
            }
            return(results);
      }
      
      public void Print(){
            System.out.println("The card I see is called " + Text);
      }

}

public class Bucket{
      DCard Head;
      DCard Current;
      DCard Temp;
      int Count;
      Bucket(){
            Head=null;
            Temp=null;
            Current=Head;
            Count=0;
      }
      public void Clear(){
            
            Head=null;
            Temp=null;
            Current=Head;
            Count=0;
      }
      public void Add(DCard data){
            Current=Head;
            Temp=null;
            while(!Current.EndofLine()){
                  Current=Current.NextCard;
            }
            if(Current.EndofLine()){
                  Temp=data;
                  Current.NextCard=Temp;
                  Temp.NextCard=null;            
            }
            
      }
      
      public void Add(String data){
            String tmpdata=data;
            DCard tmpdatacrd=new DCard(tmpdata);
            Current=Head;
            Temp=null;
            while(!Current.EndofLine()){
                  Current=Current.NextCard;
            }
            if(Current.EndofLine()){
                  Temp=tmpdatacrd;
                  Current.NextCard=Temp;
                  Temp.NextCard=null;            
            }
            
      }
      
      
}

}

SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Initialially its full of nulls, you need to popuylate it


        for(j = 0; j < 64; j++)
        {
               
                Buckets[index] = new Bucket();   // <-- add this



                make_string();
                index = hash(str);
                if ( 0 == hash_table[index])
                        free_hash--;
                hash_table[index]++;
                String tmpstr=str;
               
                Buckets[index].Add(tmpstr);
        }
>                 Buckets[index] = new Bucket();   // <-- add this

typo should be:

       for (int i=0; i<Buckets.length; i++)
            Buckets[i] = new Bucket();

        for(j = 0; j < 64; j++)
        {
                        make_string();
                index = hash(str);
                if ( 0 == hash_table[index])
                        free_hash--;
                hash_table[index]++;
                String tmpstr=str;
               
                Buckets[index].Add(tmpstr);
        }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of the_b1ackfox

ASKER

When I try to create a new bucket, I get :  No enclosing instance of type hashtable is accessible...  What am I missing?
your Bucket class is in inner class of Hashtable, though it doesn't appear to need to be
move it out of Hashtable or make it static
though the better solution would be to get rid of all that static, but thats more work ;)
>>
When I try to create a new bucket, I get :  No enclosing instance of type hashtable is accessible...  What am I missing?
>>

See my preceding comment
import java.util.*;

public class HashTable
{
      static int ccount;

      static String str;
      // static String str2;
      static int hash_table[] = new int[64];
      static Bucket Buckets[] = new Bucket[64];

      public static void main(String[] args)
      {
            int j;
            int index = 0;
            int free_hash = 64;

            System.out.println("64 random strings...");
            System.out.println();

             for (int i=0; i<Buckets.length; i++)
                  Buckets[i] = new Bucket();
            
             for (j = 0; j < 64; j++)
            {

                  make_string();
                  index = hash(str);
                  if (0 == hash_table[index])
                        free_hash--;
                  hash_table[index]++;
                  String tmpstr = str;

                  Buckets[index].Add(tmpstr);
            }

            System.out.println();
            System.out.println();
            System.out.println("Hash Table...");
            System.out.println();

            for (j = 0; j < 64; j++)
            {
                  System.out.print(hash_table[j] + "\t");
                  if (7 == j % 8)
                        System.out.println();
            }
            System.out.println();
            System.out.println("The number of free locations is " + free_hash);
      }

      static int hash(String name)
      {
            int middle, hash_value;

            middle = name.length() / 2;
            hash_value = name.charAt(0) + name.charAt(middle)
                    + name.charAt(name.length() - 1);
            hash_value %= 64;
            System.out.print("hash = " + hash_value + "\t");
            if (++ccount == 3)
            {
                  ccount = 0;
                  System.out.println();
            }
            return hash_value;
      }

      static void make_string()
      {
            int i, k;

            Random genran = new Random();

            do
            {
                  k = genran.nextInt(6);
            }
            while (k == 0);
            str = "";
            for (i = 0; i < k; i++)
            {
                  str += (char) (65 + genran.nextInt(25));
            }
            System.out.print(str + "\t");
      }

      static class DCard
      {
            DCard NextCard;
            String Text;

            DCard()
            {
                  NextCard = null;
                  Text = "";
            }

            DCard(String txt)
            {
                  Text = txt;
                  NextCard = null;
            }

            public boolean EndofLine()
            {
                  boolean results = false;
                  if (NextCard == null)
                  {
                        results = true;
                  }
                  return (results);
            }

            public void Print()
            {
                  System.out.println("The card I see is called " + Text);
            }

      }

      static class Bucket
      {
            DCard Head;
            DCard Current;
            DCard Temp;
            int Count;

            Bucket()
            {
                  Head = null;
                  Temp = null;
                  Current = Head;
                  Count = 0;
            }

            public void Clear()
            {

                  Head = null;
                  Temp = null;
                  Current = Head;
                  Count = 0;
            }

            public void Add(DCard data)
            {
                  Current = Head;
                  Temp = null;
                  while (!Current.EndofLine())
                  {
                        Current = Current.NextCard;
                  }
                  if (Current.EndofLine())
                  {
                        Temp = data;
                        Current.NextCard = Temp;
                        Temp.NextCard = null;
                  }

            }

            public void Add(String data)
            {
                  String tmpdata = data;
                  DCard tmpdatacrd = new DCard(tmpdata);
                  Current = Head;
                  Temp = null;
                  while (!Current.EndofLine())
                  {
                        Current = Current.NextCard;
                  }
                  if (Current.EndofLine())
                  {
                        Temp = tmpdatacrd;
                        Current.NextCard = Temp;
                        Temp.NextCard = null;
                  }

            }

      }

}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You need a little more analysis on this. The list functionality should reside in Bucket, not DCard. If you sort that out, you'll find it simplifies your code considerably
CEHJ & Objects, It is omg its early in the morning here and I am wiped out.  I got the program working, I guess I was alil too frustrated to see the 'static' complications...   Anyways, I will divy up the points later on tonight.  You guys really came through for me last night.!
No problem
the list functionality you have in DCard is fine by the way (and a fairly standard way of implementing it) :) Changing it would actually make your code more complex.
If you have any problems with the code I posted then give me a yell and I'll help sort it out.
If the list functionality is in DCard, it shouldn't also be in Bucket. Have a look at how Sun do it in HashMap
The list functionality that is in DCard is minimal and necessary (as it is an item in a list) :-D Not exactly sure why u are carry on about it when that is a very standard implementation and is working fine.

Sun use a different approach so comparing to it isn't really that relevant (and a bit of a waste of time). The approach you are using is fine, in fact its been used by many of my students (with 100% pass rate). And it corresponds to what they have been taught and encouraged to use by their lecturers. You can rewrite it using a different approach if you want but it would seem a huge waste of time to me (and could possibly cost u marks if this is coursework)


I think you've missed the point ;-)

>>Sun use a different approach ...

They don't actually. It's essentially the same but with difference in the fine details, which are what is evidently what's confusing you.

the_b1ackfox, let me know if you need clarification
Haven't missed the point at all, in fact I know the code quite intimately. And have helped countless students implementing maps. So I think I have a pretty good understanding of whats required :)


the initial work was for an assignment.  But I am more vested in the solution than just school work.  School work is for a grade, however I am in this for the knowledge, so I need to know how to do things right.  I have opened another question for this (https://www.experts-exchange.com/questions/22951800/Using-a-hashtable-to-implement-maps.html   500 pts ) , but objects, you mentioned map implementation.  can you elaborate on this.  I mistakenly put it in the C# area, but either way really works for me.

And CEHJ what is the reference to the sun material?  I would like to review it.  The points for this question will be awarded as follows 300/200 with a grade of A    Objects examples helped me realize what was going on with the static calls
> but objects, you mentioned map implementation.  can you elaborate on this.

that is what you are doing, ie. implementing a Map
where each object is mapped to a bucket (using its hashcode)
where more than one object maps to the same bucket then a linked list is used to manage the bucket
Objects, if you will address this in https://www.experts-exchange.com/questions/22951800/Using-a-hashtable-to-implement-maps.html I'd love some more detail.  I hate to say that the question is kinda tailored to you specifically, but, It almost *is*.  I didn't know that you could implement maps before this thread... but like I said..  I am vested in this, and interested
>>And CEHJ what is the reference to the sun material?  I would like to review it.  

Have a look at the source code for java.util.HashMap. In addition to learning a lot if you take the time to understand it, you might find it will change your approach to points distribution on this question if you do ;-)
Actually CEHJ, that may lead to a question tailored for you as well.  I will look up the reference material and get back to you.  I will post the url here if there is a tailored question
Well we don't really do 'tailoring' here ;-). It's a free-for-all, which is actually better from your POV