Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Lists of Sets and Sets of Lists

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Lists of Sets and Sets of Lists

    Write a program that reads several lines of integers. Each line of integers should be stored in
    an object of type List<Integer>, and all the lines should be stored in an object of type
    HashSet<List<Integer>>. Then the program should print out the contents of this set.


    import java.util.*;
     
    class q2
    {
     public static void main(String[] args) throws Exception
     {
      String morelines = "Yes";
      int j=0;
      while (!morelines.equals("No")) {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter some integers (all on one line):");
      String linej = input.nextLine();
      String[] numsj = linej.split(" ");
     
      List<Integer> aj = new HashSet<List<Integer>>();
      for(int i=0; i<numsj.length; i++)
         aj.add(Integer.parseInt(numsj[i]));
     
      System.out.println("Do you want to enter some more lines? Yes or No");
      morelines = input.nextLine();
     
      j=j+1;
      }
      for (int i=0; i<j; i++)
      System.out.println("The integers are: "+ai);
     }
    }

    Whats wrong with the code?
    Last edited by Freaky Chris; December 8th, 2009 at 07:09 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Lists of Sets and Sets of Lists

    You cannot assign a HashSet to a List

    List<Integer> aj = new HashSet<List<Integer>>();//???

    It'd be more appropriate to create a new list for each line, add things, then add the list to a previously created HashSet

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Lists of Sets and Sets of Lists

    HashSet< List<Integer> > aj = new HashSet<List<Integer>>();
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 02:46 AM