Help with Lists/Collections

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmstn
    New Member
    • Feb 2007
    • 16

    Help with Lists/Collections

    Okay. I'm a new user here. I want some help. I have an assignment to do. I'm good so far but I miss something. Okay:
    We have to create a public class named "Seizure". This class contains a public and static method named "imbuement" . This method has got 3 specifiers. The first is a collection(Arra ylist <Integer>), and the other 2 specifiers are arrays(type: double). That collection has the same amount of elements with the 2 arrays. This method returns a collection(type : LinkedList<Inte ger>). This LinkedList consists of the elements of the first collection for whom, the element of the first "double" array is true and the element of the 2nd array is false. For example, if "a" is the first array and "b" the 2nd, then if a[i]==true and b[i]==false, that element in the Arraylist collection (i) can be added to the LinekdList collection. I hope I've said it clearly.
    Thats what I did so far:

    import java.util.*;

    public class Seizure {
    public static LinkedList imbuement(List <Integer>x, boolean []a, boolean []b) {
    List<Integer> list1 = new ArrayList<Integ er>();
    x = list1;
    a = new boolean[list1.size()];
    b = new boolean[list1.size()];
    List<Integer> list2 = new LinkedList<Inte ger>();

    Integer p[] = new Integer[list1.size()];
    p = list1.toArray(n ew Integer[list1.size()]);

    for(int i = 0; i <= list1.size(); i++) {
    if ( a[i]==true && b[i] ==false) {
    list2.add(i, p[i]);
    }
    }
    return (LinkedList)lis t2;

    }
    }

    I compiled it and it's fine, but something's wrong and the program isn't right. I'd appreciate your help. Thanks in advance. ;)
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Code:
    import java.util.*;
    
    public class Seizure {
        public static LinkedList imbuement(List <Integer>x, boolean []a, boolean []b) {
            List<Integer> list1 = new ArrayList<Integer>();
            x = list1;
            a = new boolean[list1.size()];
            b = new boolean[list1.size()];
            List<Integer> list2 = new LinkedList<Integer>();
    
            Integer p[] = new Integer[list1.size()];
            p = list1.toArray(new Integer[list1.size()]);
    
            for(int i = 0; i <= list1.size(); i++) {
                if ( a[i]==true && b[i] ==false) {
                    list2.add(i, p[i]);
                }
            }
            return (LinkedList)list2;
        }
    }
    Take a look at the first few lines in this function. You have received three arguents x, a, and b, and you should be using these - but the first thing you do is re-assign x, a, and b to new, empty arrays/Lists. Get rid of the assignment statements and leave your code as:

    Code:
    import java.util.*;
    
    public class Seizure {
        public static LinkedList imbuement(List <Integer>x, boolean []a, boolean []b) {
            List<Integer> list2 = new LinkedList<Integer>();
            Integer p[] = new Integer[list1.size()];
            p = list1.toArray(new Integer[list1.size()]);
    
            for(int i = 0; i <= list1.size(); i++) {
                if ( a[i]==true && b[i] ==false) {
                    list2.add(i, p[i]);
                }
            }
            return (LinkedList)list2;
        }
    }

    Comment

    • dmstn
      New Member
      • Feb 2007
      • 16

      #3
      I tried it, but it's still wrong. It says that the function "imbuement" hasn't been created according to what the exercise asks.
      Mistake message: null

      Comment

      • dmstn
        New Member
        • Feb 2007
        • 16

        #4
        Ahh. It keeps saying that I haven't wrote the function as it asks.
        Is there anyone who can find my little mistake? Please read what the exercise asks. I have done pretty much everything that is needed but...

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Can you post the exact error and the current code you're working with?

          Comment

          • dmstn
            New Member
            • Feb 2007
            • 16

            #6
            Nevermind. Just fixed it. It's basically what you said and one thing. list.get(). I forgot about that. Thanks for your help, though.

            import java.util.*;

            public class Seizure {
            public static LinkedList imbuement(Array List <Integer>list 1, boolean []a, boolean []b) {
            LinkedList<Inte ger> list2 = new LinkedList<Inte ger>();
            for(int i = 0; i <= a.length -1; i++) {
            if ( a[i]==true & b[i] ==false) {
            list2.add(list1 .get(i) );
            }
            }
            return list2;
            }

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Yeah, I was wondering why you converting a perfectly good ArrayList to a native int array...thought it might have been easier for you. Good to know you got it working!

              Comment

              Working...