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 7 of 7

Thread: Need help with Vectors

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Vectors

    This is my first time doing vectors in Java and I am a bit rusty at java as it is and would appreciate any help.
    I have to have a method called
    boolean addGroup(Vector objects, int start) – This method adds the elements in the vector argument to the list, starting at position start in the list and preserving the vector order. Be mindful of the CPU performance of your solution. You may assume that the list/array will always be big enough to take the additional objects.

    This is what i have done so far in LinkList
    public boolean addGroup(Vector<Object> objects, int start)
    {
    boolean result = false;
    {
    Vector<Object>a = new Vector<Object>();
    a.addAll(objects);
    result = true;
    }
    return result;
    }
    Test:
    public void testAddGroup()
    {
    populateList();
    assertTrue(lList.getLength() == 2);
    populateVector();
    boolean result = lList.addGroup(films, 1);
    assertTrue(result);
    assertTrue(lList.getLength() == 8);
    assertEquals(film3, lList.getEntry(3));
    assertEquals(film7,lList.getEntry(7)) ;
    assertNull(lList.getEntry(9)) ;
    }
    I have created a method called populateVector() to make it easier for making my test class:
    private void populateVector()
    {
    films.add(film3);
    films.add(film4);
    films.add(film5);
    films.add(film6);
    films.add(film7);
    films.add(film8);
    }
    Last edited by Locky; October 18th, 2010 at 08:48 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,170
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: Need help with Vectors

    What's your question?

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Vectors

    My question is what can I do to make my code work. I have ran it in the test and it has just failed. I have been at this for quite a while and since it's my first time doing Linked Lists and Vectors I am very confused.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,170
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: Need help with Vectors

    Quote Originally Posted by Locky View Post
    My question is what can I do to make my code work. I have ran it in the test and it has just failed. I have been at this for quite a while and since it's my first time doing Linked Lists and Vectors I am very confused.
    Run a debugger to figure out where your code is different from what you expect. Get it narrowed down to a couple lines, then ask a specific question.

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Vectors

    Thats the problem. I am unsure where I am going wrong as this is my first attempt at Linked List and also at Vectors.
    I know this code is wrong but I would like to know if I have done anything right:

     public boolean addGroup(Vector<Object> objects, int start)
    {
    boolean result = false;
    {
    Vector<Object>a = new Vector<Object>();
    a.addAll(objects);
    result = true;
    }
    return result;
    }
    Last edited by Locky; October 18th, 2010 at 10:20 AM.

  6. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Need help with Vectors

    Your posted code is missing quite a bit of information, and posting where it fails in the test will help explain what the problem is. Based upon what you have so far, you add all the parameter objects to a Vector which has the scope of the addGroup function eg the vector is lost as soon as the function exits, which I presume is not the behavior you wish.

  7. The Following User Says Thank You to copeg For This Useful Post:

    Espressoul (October 19th, 2010)

  8. #7
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Vectors

    Did you get it sorted and submitted after Jay?

    Nice tip on using a debugger copeg, figured out how to set a breakpoint and step through/over methods to see how they work. Always wanted to see a method go through each step and watch the cogs turn, will be handy in future.

Similar Threads

  1. New to vectors passing parameters
    By osmaavenro in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2010, 04:32 PM
  2. Question regarding bit vectors...
    By ADizzle491 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 21st, 2010, 08:50 PM
  3. Vectors - accessing an unknown amount of objects
    By fox in forum Loops & Control Statements
    Replies: 1
    Last Post: May 7th, 2010, 03:54 PM
  4. Vectors
    By mgutierrez19 in forum Collections and Generics
    Replies: 4
    Last Post: March 3rd, 2010, 10:46 AM
  5. Replies: 3
    Last Post: November 15th, 2008, 06:17 AM