Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:19:01+00:00 2026-06-13T11:19:01+00:00

I am having problems with creating HashTable in my program, I am creating WordLadder

  • 0

I am having problems with creating HashTable in my program, I am creating WordLadder game.
Basically I want my HashTable to contain key which is unique word and value would be words which are one letter difference. Problem is that when I print out to check what I am putting into it, it prints perfectly what I want, however when I return HashTable it returns me nonsense.

My code for generating HashTable is following:

public Hashtable<String, ArrayList<String>> findNeighbors(){
      Hashtable<String, ArrayList<String>> data = new Hashtable<String, ArrayList<String>>();
      ArrayList<String> neighb = new ArrayList<String>();
      for(int i=0; i < 5; i++){
       for(int j=0; j < 5; j++){
        if (isNeighbor(words.get(i), words.get(j))) {
         neighb.add(words.get(j));
        }
       }
       data.put(words.get(i), neighb);
       //System.out.println(words.get(i)+ " "+data.get(words.get(i))); <This Works perfectly fine
       System.out.println(data.toString()); //<This returns nonsense
       neighb.clear();
      }

      return data;
     }

public boolean isNeighbor(String a, String b){
      int diff = 0;
      for (int i = 0; i < a.length(); i++){
       if(a.charAt(i) != b.charAt(i)){
        diff++;
       }
      }
      return diff==1;
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:19:02+00:00Added an answer on June 13, 2026 at 11:19 am

    Look at neighb.clear();, You are calling on the same object you put in your Hashtable. You are clearing your list soon after putting it in the Hashtable. May you want to create a new local arraylist, add all elements to the new arraylist add then add the new arraylist in Hashtable and then clear your neighb e.g.

          List<String> newList = new ArrayList<String>();
          newList.addAll(neighb);
          data.put(words.get(i), newList);
          newList.clear();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problems creating a table in python. Basically I want to build
I've been having problems creating a foreign key in Java Db through Netbeans. I'm
I'm having problems with creating mySQL table within Java program. I constantly get Can't
I'm having problems creating a regular expression which will fix a valid string. The
i'm having problems creating a program that converts a string of numbers into an
Hey i'm having problems creating a simple button for a programme which finds the
I'm having big problems while creating a simple program in C. I have the
I am having problems creating a simple PHP script. The $_GET[give] variable is always
Having real problems creating artifacts in teamcity 6.5 (using TFS & MSBuild as the
I have been having problems with creating a download list of files for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.