user8925274
user8925274

Reputation:

Creating constructor for hashset and hashmap

I have the following data structures created..

private Scanner scan;
HashSet<String> hset = new HashSet<String>();
HashMap<String, ArrayList<Integer>> hmap = new HashMap<String, ArrayList<Integer>>(); 
ArrayList<Integer> myList = new ArrayList<Integer>();

I am confused on how to create a constructor to initialize these. I have the scanner I am pretty sure but some help with the others would be great.

public SpellChecker(){
    scan = new Scanner(System.in);
    hset = new HashSet<String>();
    hmap = new HashMap<String, ArrayList<Integer>>();
    myList = new ArrayList<Integer>();

}

Upvotes: 0

Views: 109

Answers (1)

mckuok
mckuok

Reputation: 754

Based on your comments, you can define those variables as class member variables, so that you can use the variable in the scope that you want (with the right access modifier that is )

public class SpellChecker {
    private Scanner scan;
    private HashSet<String> hset;
    private HashMap<String, ArrayList<Integer>> hmap;
    private ArrayList<Integer> myList;

    public SpellChecker(){
        scan = new Scanner(System.in);
        hset = new HashSet<String>();
        hmap = new HashMap<String, ArrayList<Integer>>();
        myList = new ArrayList<Integer>();
    }

}

Upvotes: 1

Related Questions