The Artima Developer Community
Sponsored Link

Java Answers Forum
How to get thing from set

2 replies on 1 page. Most recent reply: May 18, 2002 10:56 AM by Evgen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Linda

Posts: 2
Nickname: lapo35
Registered: Apr, 2002

How to get thing from set Posted: May 16, 2002 10:09 PM
Reply to this message Reply
Advertisement
Hi all I get some trouble with set.....
1.I want to fill my set with 50 random number but it doesn't print 50 number just 19 number depend on how many duplication.....would you all suggest the best idea to solve it....all answer will be appreciate
public static void fillSet(Set number)
{
for(int a=0; a < 50;a++)
{
Integer num = new Integer((int) (11 +Math.random()*19));
number.add(num); }
Iterator myI = number.iterator();
while(myI.hasNext())
{
System.out.println(myI.next());
}
}//method fillSet
Thank's alot


Rohit

Posts: 38
Nickname: rohit
Registered: May, 2002

Re: How to get thing from set Posted: May 16, 2002 10:51 PM
Reply to this message Reply
Whenever you are adding any thing to set , it will be added only if its not there allready,well that's what a set should be.

Evgen

Posts: 14
Nickname: evgen79
Registered: May, 2002

Re: How to get thing from set Posted: May 18, 2002 10:56 AM
Reply to this message Reply
import java.util.*;
 
class MySet
{
   public static void main(String[] args) {
      HashSet number = new HashSet();
      fillSet(Set number);
   }
 
   public static void fillSet() {
      
      MyInteger myInt;
      for(int a=0; a < 10; a++) { 
         Integer num = new Integer((int) (11 +Math.random()*5));
         myInt = new MyInteger(num);
         System.out.print(num + " ");
         number.add(myInt); 
      }
 
      System.out.println();
 
      Iterator myI = number.iterator();
      while(myI.hasNext()) {
         System.out.print(myI.next() + " ");
      }
   }
}
 
class MyInteger {
   Integer i;
 
   public String toString() {
      return String.valueOf(i);
   }
 
   public MyInteger(Integer ni) {
      i = ni;
   }
}

Flat View: This topic has 2 replies on 1 page
Topic: java Previous Topic   Next Topic Topic: I always got exception:java.lang.NoClassDefFoundError:

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use