• 0

[JAVA] new array of strings question


Question

i have an array of states.....what happens is that i read in all these objects and many of them could be from the same state...but i just want to count each state once...

like if i have 3 people from iowa, 2 from new york and 3 from california, my states array will only have 3 elements in it, iowa, new york and california.....

i need the array to be exactly the size of the number of states, and nothing bigger or when i try to sort it, it will throw a null pointer exception....how can i count the number of states i will have....i have a different array that has the states and positions of records in them so i could use those somehow....

any ideas?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

ok....i'm still having major difficulties and this is the very last part....

once i add the elements (in this case strings) to the HashSet, can i take that set and make an array out of it? how could i do that ?

i'm still getting to where i have duplicate elements in my states array....

Link to comment
Share on other sites

  • 0

There's a toArray() method on a HashSet object.

HashSet hashSet;
//put items in the HashSet
...

String[] states = new String[hashSet.size()];

hashSet.toArray(states);

for(int i=0;i<states.length;i++)System.out.println(states[i]);

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.