Reversing a list

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 744 Views - Last Post: 20 September 2011 - 08:06 PM Rate Topic: -----

#1 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Reversing a list

Posted 20 September 2011 - 10:54 AM

Post deleted

This post has been edited by imu_1: 20 September 2011 - 11:11 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Reversing a list

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Reversing a list

Posted 20 September 2011 - 11:01 AM

Have you tried to do a little bit of debugging? Like put a System.out.println(stack.empty()); line after the for loop that pushes them in. That way you know if it is empty or not. Then we can go from there

Edit:
It ran for me after I changed one thing:
public static void reverse(List<Integer> list)
to
public static void reverse(ArrayList<Integer> list)

This post has been edited by Fuzzyness: 20 September 2011 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: Reversing a list

Posted 20 September 2011 - 11:02 AM

Have you actually stepped through the code to verify that it has values in it?
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Reversing a list

Posted 20 September 2011 - 11:14 AM

Erm.. I guess since you deleted the post you solved it?

Good luck
Was This Post Helpful? 0
  • +
  • -

#5 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Reversing a list

Posted 20 September 2011 - 11:23 AM

No i havent
I thought i have

I did stack.size(), i got 4
I did stack.empty(), i got false,
I printed out the stack by pop methos, I got 40,30,20,10

But when I put the number in the list, I am not getting the list reversed

public static void reverse(List<Integer> list) 
 { 
     Stack<Integer> stack = new Stack<Integer>(); 
	 
	 for(int i = 0; i < list.size(); i++) 
	 { 
	    stack.push(new Integer(list.get(i)));
	 } 
	 
	 //for(int i = 0; i < list.size(); i++) 
	 //{ 
	    //list.add(i,stack.pop());
	   
	  // System.out.println(stack.pop()); 
	 //}
    

}





Up to that point, I get 40,30,20,10
But then when I insert it in the list, I am not getting it reversed

This post has been edited by imu_1: 20 September 2011 - 11:20 AM

Was This Post Helpful? -3
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Reversing a list

Posted 20 September 2011 - 11:27 AM

View Postimu_1, on 21 September 2011 - 12:04 AM, said:

No i havent
I thought i have

I did stack.size(), i got 4
I did stack.empty(), i got false,
I printed out the stack by pop methos, I got 40,30,20,10

But when I put the number in the list, I am not getting the list reversed


So what can we do then with this then?!:

View Postimu_1, on 20 September 2011 - 11:39 PM, said:

Post deleted


It is not a good idea to delete the post because you get the answer. This is an online community, people help each other so the help will remain reference in future for those who are going to face the same problem... Just an advice
So now we have nothing to do with your problem because of what you did
Was This Post Helpful? 1
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8940
  • View blog
  • Posts: 33,530
  • Joined: 12-June 08

Re: Reversing a list

Posted 20 September 2011 - 11:28 AM

Please do not delete your post. That breaks the continuity for the answers and solutions presented, and is rude to those who have a similar question in the future and those who have helped you now.

Locking a now useless thread.
Was This Post Helpful? 2
  • +
  • -

#8 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Reversing a list

Posted 20 September 2011 - 11:35 AM

Alright guys , am sorry.

I apologize for this act... @smohd, A special apology o u cauz looks like u r the most hurt.

Here's the code.


ublic static void reverse(List<Integer> list) 
 { 

     public static void main (String [] args) 
   { 
       ArrayList<Integer> list = new ArrayList<Integer>(6); 
	   
	   Integer i1=new Integer(10);
      Integer i2=new Integer(20);
      Integer i3=new Integer(30);
     Integer i4=new Integer(40);
	 list.add(i1); 
	 list.add(i2); 
	 list.add(i3); 
	 list.add(i4); 
	 
	    //System.out.println(list);  
	    //System.out.println(list.get(1));
	   reverse(list);
	} 
     Stack<Integer> stack = new Stack<Integer>(); 
	 
	 for(int i = 0; i < list.size(); i++) 
	 { 
	    stack.push(new Integer(list.get(i)));
}	 } 





If you wanna help, I'll appreciate it.

This post has been edited by macosxnerd101: 20 September 2011 - 11:38 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10392
  • View blog
  • Posts: 38,456
  • Joined: 27-December 08

Re: Reversing a list

Posted 20 September 2011 - 11:41 AM

Since you have added your code back, I've merged your threads and re-opened the closed one. What specific problems or errors are you encountering?

Just a note- you cannot define a method within another method. And do you have to use a Stack? Collections.reverse() is a built-in method that can do this.
Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Reversing a list

Posted 20 September 2011 - 11:44 AM

If I see correctly you have main() inside a reverse method?
What you did is just pushing to the stack to reverse you have to pop them and assign again to the list
Was This Post Helpful? 0
  • +
  • -

#11 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Reversing a list

Posted 20 September 2011 - 11:48 AM

Sorry, That was a typo. My main method is different .

Here it is:

 public static void main (String [] args) 
   { 
       ArrayList<Integer> list = new ArrayList<Integer>(6); 
	   
	   Integer i1=new Integer(10);
       Integer i2=new Integer(20);
       Integer i3=new Integer(30);
       Integer i4=new Integer(40);
	   list.add(i1); 
	   list.add(i2); 
	   list.add(i3); 
	   list.add(i4); 
	 
	    //System.out.println(list);  
	    //System.out.println(list.get(1));
	   reverse(list);
	} 





The issue is in my reverse method. I have all the elements in my stack and I only want to put them in the list. But when i insert them, i dont get a reversed list.
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Reversing a list

Posted 20 September 2011 - 12:08 PM

I dont see what you are doing is a good way, but you need to pop the value and reassign to the list. Then you will have your list reversed. Like:
int x = stack.size();  
for(int i = 0; i < x; i++) 
	 { 
             list.add(i,stack.pop());
         
         }

EDIT:Code corrected

This post has been edited by smohd: 20 September 2011 - 12:35 PM

Was This Post Helpful? 3
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10392
  • View blog
  • Posts: 38,456
  • Joined: 27-December 08

Re: Reversing a list

Posted 20 September 2011 - 12:09 PM

Make sure to clear your List first. Otherwise, you will have the elements in reverse order AND the elements in the original order. You can use the set(index, elem) method instead of the add() method as well (which is really more efficient). :)
Was This Post Helpful? 2
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Reversing a list

Posted 20 September 2011 - 12:21 PM

View Postsmohd, on 20 September 2011 - 01:08 PM, said:

I dont see what you are doing is a good way, but you need to pop the value and reassign to the list. Then you will have your list reversed. Like:
  for(int i = 0; i <= stack.size(); i++) 
	 { 
             list.add(i,stack.pop());
         
         }

Actually this won't get all of the numbers.
Stack: 40,30,20,10
Size: 4
iteration one, adds 40, stack size is now 3, int i is 1. adds 30, size is 2 and int i is 2 meeting the conditional for the if statement.

What you need is good practice in java, jon.kiparsky grilled me for it a week ago :sigh:

1. Create a int counter at beginning of method that takes the arraylist size as the value. This will also make it so that you dont have to perform the .size() method each iteration

2. Create the for loops with the conditional being i< count that you created

3. Do as macosxnerd101 said and call the arraylist clear method AFTER you add the items to the stack

4. Proceed to use line you had before and add the list at i with stack.pop();

You will have a reversed Arraylist. You can print it out for proof. When you print it out you cannot just use System.out.println(list); that will just print the reference. You need to make a for each loop or a for loop to cycle through and print out each number.

make sense?

This post has been edited by Fuzzyness: 20 September 2011 - 12:23 PM

Was This Post Helpful? 1
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10392
  • View blog
  • Posts: 38,456
  • Joined: 27-December 08

Re: Reversing a list

Posted 20 September 2011 - 12:24 PM

@smohd: Also, watch out for that operator. It's <, not <= for Collections. :)
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2