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

Thread: ADT list help

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

    Default ADT list help

    Hey, i'm having trouble understanding what to do for the following. I have to do a swap method:
    public boolean swap(Object one, Object two)
    I'm doing it in Alist, using an array. I'll have to check whether they are present also?
    I haven't done java in a while so I'm finding it difficult to know what to do. Any help appreciated, thanks.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ADT list help

    Ok, first off, an array is very vague. Is it an array in the sense of: Object[] or an array in the sense of a Collection<E>? Knowing that will tell us how to go about swapping.

    I would assume this is an Object[]. So, you need to find each Object in the Array and note their indexes first. If you can't find one of them, I would guess you return false. If you find both, you will need to swap the two by:
    1) Set a temp variable whose value is one
    2) Set one's index to have the value of two
    3) Set two's index to have the value of the temp variable

    Pretty straight forward. See if you can code that yourself instead of me providing the code. I will help you out with any coding issues you have if you provide the code when you have trouble.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Default Re: ADT list help

    Hey, thanks for replying. I've posted what i have so far. I know the "isSuccessful" isn't changing or anything, just wondering if i'm on the right track. thanks again



     public boolean swap(Object one, Object two)
        {
            boolean isSuccessful = false;
            Object temp = entries[0];
            Object temp2 = entries[0];
            int indexOne = 0;
            int indexTwo = 0;
     
            for(int i = 1;(i<nextFreeIndex) && temp != one;  i++)
            {
                temp = entries[i-1];
                indexOne = i;
            }
     
            for(int i = 1; (i<nextFreeIndex) && temp2 != two; i++)
            {
                temp2 = entries[i-1];
                indexTwo = i;
            }
     
            Object x = one;
            entries[indexOne] = two;
            entries[indexTwo] = x;
     
     
     
            return isSuccessful;
        }

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ADT list help

    Seems good. There are a few stylistic things I would change:

    First off, there is no real reason to loop through twice. You can actually loop once and just check each value each time. We can actually also avoid using booleans and objects here. You know that your index will never equal anything lower than 0, so when looking for an index, most people set the default value to -1. If, after the looping, the index is still -1, you know it wasn't found. Otherwise, it was found. So, you could change:
    Object temp = entries[0];
            Object temp2 = entries[0];
            int indexOne = 0;
            int indexTwo = 0;
     
            for(int i = 1;(i<nextFreeIndex) && temp != one;  i++)
            {
                temp = entries[i-1];
                indexOne = i;
            }
     
            for(int i = 1; (i<nextFreeIndex) && temp2 != two; i++)
            {
                temp2 = entries[i-1];
                indexTwo = i;
            }
    into:
    //Default Index Values
    int indexOne = -1;
    int indexTwo = -1;
    //Loop through Array       
    for(int i = 0;i<nextFreeIndex;i++)
    {
    	/* If indexOne has not been set and the current Object is
    	 * equal to Object one, set indexOne. Note the order of the
    	 * conditions in the if statement. This is to maximize 
    	 * efficiency. When JAVA checks the statements that include
    	 * AND, if the first statement is false, it doesn't even 
    	 * evaluate the proceeding conditions. Doing it this way 
    	 * increases efficiency because it doesn't need to check entries[i]
    	 * and one for equality if indexOne has been set. Also note the
    	 * use of .equals to compare objects instead of the == for numbers
    	 */
    	if(indexOne==-1 && entries[i].equals(one))
    	{
    		//Set IndexOne
    		indexOne = i;
    		/* Check if indexTwo has been set. If it has, then we
    		 * know we have found both of our variables and we can
    		 * stop looping. We stop the loop with the break; statement.
    		 */
    		if(indexTwo!=-1)
    			break;
    	}
    	//Check if indexTwo has been set.
    	if(indexTwo==-1 && entires[i].equals(two))
    	{	
    		//Set IndexTwo
    		indexTwo = i;
    		//Check if indexOne has been set.
    		if(indexOne!=-1)
    			break;
    	}
    	/* This will continue to loop either both variables have been set or
    	 * the loop reaches the end. We do not check indexOne and indexTwo for
    	 * being set after the if statements, because that means the check will
    	 * occur each time the loop is ran. By having the checks in each if
    	 * statement, it only checks both variables when at least one of them
    	 * has been found.
    	 */
    }
    //Check if either indexes were not found. If one wasn't found, return false.
    if(indexOne==-1 || indexTwo==-1)
    	return false;

    I heavily commented so you have a complete understanding for why I chose the stylistic procedure I did.

    Once again, your code should work fine. But I'm pretty sure that my code is faster (by a few milliseconds maybe, nothing substantial). I really just wanted to give you an example of another way of doing it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    jkalm (December 3rd, 2010)

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ADT list help

    Hey thanks a lot. Really helped me out. I added this to the end and all seems ok. I made tests for it and there doesn't seem to be any problems, thanks again.

    		//Checks if either indexes weren't found. If one wasn't, returns false
    		if(indexOne == -1 || indexTwo == -1)
    			isSuccessful = false;
     
    		//Swap objects if both indexes were found
    		else
    	    {
    	    	Object x = one;
    	    	entries[indexOne] = two;
    	    	entries[indexTwo] = x;
    	    	isSuccessful = true;
    	    }
     
    	    return isSuccessful;

  7. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ADT list help

    Hey I now have to do: boolean addGroup(Vector objects, int start). Really don't know where to start, never done vectors before.
    The description is : – 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.
    Any help with where to even start would be appreciated, thanks.

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ADT list help

    Ok, well Vectors are very simple.

    I'm sure you are not allowed to, but Vectors actually support this feature already. The method: Vector.copyInto(Object[] anArray) is provided by the Vector class. It takes all the Objects from the Vector and copies them into the array. Unfortunately, I'm sure you aren't allowed to use this method, as that would render your task useless.

    You could also do: Vector.toArray(), which turns the Vector into an array, and then use the System.arraycopy(Object sourceArray,0,Object destinationArray,destinationArray.length,soureArra y.length) to put all the elements from your source array into your destination array. However, I'm sure you can't do that either.

    So, we will do it the long way.
    There are 2 significant methods for you to achieve what you are doing.

    1) Vector.get(int index)
    2) Vector.remove(int index)

    You need to decide which one to use here. The Vector.get(int) method will get an object from the indicated index (where index is 0 to Vector.size()-1 inclusively) but keep the value in the Vector. The Vector.remove(int) method will get an object from the indicated index, but remove it from the Vector while doing so.

    Once you have the object, you need to add each to the array. The problem is the indexOutOfBounds errors you may get while adding. So, we will have to resize the array when we add Objects. But, it is just plain inefficient and tedious to add an extra spot in the array each time you want to add an item. So, we can make this more efficient. So, here are our steps:
    1) The first thing you should do in your method is resize the array to be equal to the size of the current array plus the size of our Vector. You can get the size of the Array by saying Object[].length;. You can get the size of the Vector by saying Vector.size();.
    2) Once you have the size of the total finished array, we should make a new array with that length.
    3) Now we need to copy all the elements from our current array into our new array.
    4) Now we need to go through the Vector (by using one of the methods I mentioned above) and grab each element and put them into our new array.
    5) The last thing you need to do is return our new finished array so we can override the previous array.

    Fairly simple. See what code you can make and i'll help you from there.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ADT list help

    Hey thanks for replying again. Do we have to resize the array even though it says "You may assume that the list/array will always be big enough to take the additional objects."? or is it just something that needs to be done? also, how would i be creating the vector? is it outside the method or inside? is it correct that the user would be entering what goes into the vector?
    Last edited by jkalm; October 14th, 2010 at 08:47 AM.

  10. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ADT list help

    Ok, well if the list or array will be guaranteed big enough, we may not need to resize the array. If you run into an indexOutOfBounds exception, then that will be an indication that the array needed to be resized. But we can deal with that when that happens.

    Inside this method, you do not need to create a Vector. Notice in the method declaration: addGroup(Vector objects, int start) how a Vector is being sent to this method. It is safe to assume that the Vector being sent to the method has been initialized and contains Objects. So, you do not need to worry about adding Objects or constructing the Vector. The Vector that this method will use will be created before this method is called in some other location in your program.

    Now that I think about it, you could actually use the remove() method without destroying the order of the Vector sent to it. That is because (and I may be wrong here) scope says we cannot make lasting changes to the Vector that was sent to the method. We can make temporary changes, but those changes will disappear once the method is finished. The difference between using the get() method and the remove() method is how you go through the Vector. With the get() method, you have to increment the index being accessed each time you get an Object. With the remove() method, you do not need to increment the index because when an Object is removed, the next Object takes its index (shifting each time effectively).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ADT list help

    thanks, I'll give it a go and get back to you.

  12. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ADT list help

    I've gotten this so far. I'm not sure if it will just return true no matter what happens though. Also, I'm getting warnings saying Vector is a raw type.

    	public boolean addGroup(Vector objects, int start)
    	{
    		for(int i = 0; i <= objects.size(); i++)
    		{	
    			Object x = objects.get(i);
    			for(i = start; i <= objects.size(); i++)
    			{
    				entries[i] = x;
    			}
     
    		}
     
    		return true;
    	}

  13. #12
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ADT list help

    I'm not sure if it will just return true no matter what happens though.
    It will always return true for what you have.

    Also, I'm getting warnings saying Vector is a raw type.
    This may be (and I'm not positive) because it has not been specified what is in the Vector. In order to do that, instead of:
    public boolean addGroup(Vector objects, int start)
    you would say:
    public boolean addGroup(Vector<Object> objects, int start)

    The thing between the equal than and greater than signs (<...>) specifies to the Vector what is in the Vector. Object essentially says anything.

    For example, if you wanted to restrict a Vector to contain only Strings, you would create the Vector of Strings by saying:
    Vector<String> object = new Vector<String>();

    Considering this is only a warning, it is not entirely necessary that we have to pay attention to it. A warning just says that something bad could happen at runtime, but not that something will happen. Your professor might be expecting the warning to be thrown since it essentially means nothing. That might be something to ask.


    Meanwhile, there is a better way to do this. With some creative designing, you only need to loop once. Since we know where we want to start, we can use the counter in our loop to offset our location in our array in retrospect to start. So, you could also say:
    public boolean addGroup(Vector objects, int start)
    {
            for(int i = 0; i < objects.size(); i++)
            {  
            	Object x = objects.get(i);
                    entries[start+i] = x;
     
            }
            return true;
    }
    Keep in mind, this is just another way of doing it. Your way may work as well.

    One more issue you will have is the statement:
    for(int i = 0; i <= objects.size(); i++)
    The problem is the less than or equals to. You do not want to equal the size of the Vector because that value is out of the index range. Remember, the index range goes from 0 to object.size()-1. So, you instead just want i to be less than the size, not less than or equal to.
    Last edited by aussiemcgr; October 14th, 2010 at 03:03 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Default Re: ADT list help

    Having some trouble with the addGroup method. Really can't see where it's failing.

    This is the code
    	public boolean addGroup(Vector<Object> objects, int start)
    	{
     
    		for(int i = 0; i < objects.size(); i++)
    		{	
    			Object x = objects.get(i);
    			entries[start+i] = x;
    		}
     
    		return true;
    	}

    This is my test
    	public void addGroup()
    	{
    		Vector<Object> objects = new Vector<Object>();
    		objects.add(film1);
    		objects.add(film2);
    		objects.add(film3);
    		assertEquals(film1, objects.get(0));
     
    		aList.addGroup(objects, 1);
    	    assertTrue(aList.contains(film3));
     
    	}

    My test is failing on the last line which means the method isn't adding the objects in the vector to my aList. Any help would be appreciated.

  15. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ADT list help

    Quote Originally Posted by jkalm View Post
    My test is failing on the last line which means the method isn't adding the objects in the vector to my aList. Any help would be appreciated.
    How is your contains() method defined? Do you need to increment a size value when adding objects to 'entries'?

  16. #15
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ADT list help

    The contains method is working, I put in a new contains in the test and that one works ok. It's still the last one that fails. We're allowed assume there will always be enough room.. I'm not sure if that's what you were asking. Here's the new line in the test:
    	public void addGroup()
    	{
    		Vector<Object> objects = new Vector<Object>();
    		objects.add(film1);
    		objects.add(film2);
    		objects.add(film3);
    		assertEquals(film1, objects.get(0));
    		assertTrue(objects.contains(film1));
     
    		aList.addGroup(objects, 1);
    	    assertTrue(aList.contains(film3));
     
    	}

    The first contains in the test passes, the last line doesn't.

  17. #16
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ADT list help

    Not asking if the contains is working, but rather how it works? What I am saying is that if this list is implemented in such a way that there is a variable which contains the actual size of valid objects, AND this variable is incremented/decremented when an object is added/removed, AND the contains method uses this size variable to loop through the array to check for the object, if you add objects without incrementing this contains method will not know the actual length. Is there a method to add a single object, and does this pass the tests? Try using this to add the individual object as you loop through
    public boolean addGroup(Vector<Object> objects, int start)
        {
     
            for(int i = 0; i < objects.size(); i++)
            {  
                Object x = objects.get(i);
                add(x);
            }
     
            return true;
        }

Similar Threads

  1. list in JSP
    By smackdown90 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 13th, 2011, 01:08 PM
  2. need help with a list class
    By araujo3rd in forum Object Oriented Programming
    Replies: 1
    Last Post: February 25th, 2010, 07:58 PM
  3. Doubt regarding LIST
    By puneetsr in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2010, 04:19 PM
  4. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 04:48 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM