Saturday, 15 September 2012

collections - Set and HashSet Java -



collections - Set and HashSet Java -

i'm relatively new java language , have project i'm doing school in have book class has normal setters/getters, constructors, , overrides class, nil complicated. have alter can multiple authors utilizing set , hashset. question have how go doing this? far, , right me if i'm wrong, have this

import java.util.*; public class book{ private set<string> authorset; private string isbn; public book(){ authorset = null; isbn = null; } public book(string isbn, set<string> authorset){ this.isbn = isbn; set<string> s = new hashset<string>(); // do else here? } public string getisbn(){ homecoming isbn; } public void setisnb(string isbn){ this.isbn = isbn; } public set<string> getauthorset(set<string> newauthorset{ homecoming newauthorset; } public void setauthorset(set<string> newauthorset){ this.authorset = newauthorset; }

before moving on overrides, want create sure properly. i've tried similar examples can see what's going on, haven't had much luck of yet. i'm sure it's simple, i'm starting larn language. help

first of all, in default constructor, rid of

authorset = null;

and instead initialize authorset variable new hashset. reason want create authorset container regardless of whether authors added begin with.

you'll want constructor takes isbn string. consider constructor takes isbn string , variable number of author strings.

ah, missed this:

public book(string isbn, set<string> authorset){ this.isbn = isbn; set<string> s = new hashset<string>(); // do else here? }

not ignore both parameter , field! instead, assign set parameter existing field other field.

public book(string isbn, set<string> authorset){ this.isbn = isbn; this.authorset = authorset; }

then give class addauthor(string author) method. improve code since homework. don't think there's whole lot more need regards problem.

java collections set hashset

No comments:

Post a Comment