Avoid to print, Save the Tree.
Jai Shri Krishna,
package MyHashSet;
import java.util.HashSet;
/**
*
* @author Trey-Asus
*/
public class Main {
public static void main (String [] args)
{
HashSet hs = new HashSet(5);
hs.add("String first Object");
hs.add("String second Object");
MyOwnClass moc = new MyOwnClass("Name", 3);
MyOwnClass moc1 = new MyOwnClass("Name2",6);
hs.add(moc);
hs.add(moc1);
// Add to Integer Object
hs.add(11);
hs.add(22);
Iterator i = c.iterator();
while(i.hasNext() )
{
System.out.println(i.next();
}
//Same you apply for LinkedHashSet, ArrayList
}
}
/*
============================================================================
=
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MyHashSet;
class MyOwnClass{
String name;
int age;
public MyOwnClass (String name, int age)
{
this.name = name;
this.age= age;
this.first = first;
this.second = second;
this.third = third;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
Dil se Kaho "Aal... izzz... Well..."
Mehul
M.No: 94284 58874
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Cecil H
Sent: 02 April 2010 AM 08:11
To: Java Programming Online Training Course By Sang Shin
Subject: [java programming] Lab 1016 - First Exercise
At the end of the first exercise Mr. Sang suggested we try the
following
4. For your own exercise, please do the following
Create your own NetBeans project named as MyHashSet
Create your own HashSet object with initial capacity of 5
Add the following objects to the newly created HashSet object
2 String objects
2 MyOwnClass object (You will have to create MyOwnClass.java first)
3 Integer objects
Display the HashSet object
****** I'm a little confused by the HashSet data structure
This is what I came up with but I have doubts. Can any one tell me if
I am on the right track or explain it to me a little more.
I have posted the the code and results below:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MyHashSet;
import java.util.HashSet;
/**
*
* @author Trey-Asus
*/
public class Main {
public static void main (String [] args)
{
HashSet hs = new HashSet(5);
MyOwnClass moc = new MyOwnClass("FirstName", "LastName",
1,2,3);
MyOwnClass moc1 = new MyOwnClass("FirstName2", "LastName2",
4,5,6);
System.out.println(" " + hs.add(moc.getFName()));
System.out.println(" " + hs.add(moc.getLName()));
System.out.println(" " + hs.add(moc.getFirstNumber()));
System.out.println(" " + hs.add(moc.getSecondNumber()));
System.out.println(" " + hs.add(moc.getThirdNumber()));
System.out.println(" " + hs.add(moc1.getFName()));
System.out.println(" " + hs.add(moc1.getLName()));
System.out.println(" " + hs.add(moc1.getFirstNumber()));
System.out.println(" " + hs.add(moc1.getSecondNumber()));
System.out.println(" " + hs.add(moc1.getThirdNumber()));
// Print out the HashSet object
System.out.println(hs);
}
}
/*
============================================================================
=
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MyHashSet;
class MyOwnClass{
String fName;
String lName;
int first;
int second;
int third;
public MyOwnClass (String fName, String lName, int first, int
second, int third)
{
this.fName = fName;
this.lName = lName;
this.first = first;
this.second = second;
this.third = third;
}
public String getFName()
{
return fName;
}
public String getLName()
{
return lName;
}
public int getFirstNumber()
{
return first;
}
public int getSecondNumber()
{
return second;
}
public int getThirdNumber()
{
return third;
}
}
/
****************************************************************************
*/
Results
run:
true
true
true
true
true
true
true
true
true
true
[1, 2, 3, 4, 5, 6, FirstName2, LastName2, FirstName, LastName]
--
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe, reply using "remove me" as the subject.