7 Replies - 750 Views - Last Post: 29 November 2008 - 04:08 PM Rate Topic: -----

#1 kiboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

unable to use LinkedList class - am totally lost.

Posted 27 November 2008 - 12:17 PM

I have to convert a linked list into a String[] without using the LinkedList class...and I assume that means I can't use the toArray() method either?

My List class is terribly piecey and long - so I'm going to cut & paste what I believe the root of this particular problem is....


Here is the method I am trying to write:

private Item item; //creates new Item: status, name, price
private List A = new List(); //creates a new List (which is an inner class)

public class List
	{
		public List() //makes empty list - all I have so far
		{
			head = null;
		}

	}

public String[] whatWeHave(int user) 
{		
	   //on hand counts the number of items in the list
	String[] weHave = new String[onHand()];
   
		int size = 0;
			
		Node current = head;
		Node previous = null;
	while(size < weHave.length)
	   {
			 if(user == 0) //user input - 0 = everything we have in stock
			 {
			 //am also unsure how to sort status - use a for loop?
//thought about putting this in somewhere:
//current.item.getStatus().equals("i"); and if true add to the list -
//I guess that's a different method?
// status is either: i = in stock, o = need to order			
			
			A.toArray(weHave);	
			}
	}					
			return weHave;
}



ANY ideas, hints, links, references - will be greatly appreciated!!!
Thank you in advance for reading this!

This post has been edited by kiboko: 27 November 2008 - 12:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: unable to use LinkedList class - am totally lost.

#2 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: unable to use LinkedList class - am totally lost.

Posted 27 November 2008 - 01:20 PM

So, you have linked list of things that are either in stock or not, and you want to add whats in stock to String[], am I right?

It's often convenient to use recursion when your dealing with linked structures.

        
     public String[] whatWeHave(int user)  {
          String[] s = new String[onHand];
          traverse(0, s, head);
          return s;
     }
     //helper function, traversing the list with recursion
     public void traverse(int i, String[] s, Node node) {
             if(n == null)
                   return;
             if( the data in node  in stock) { //pseudo code
                   s[i] = data;
                   i++;
              }
              traverse(i, s, node.next()); //on to the next node;
      }



something like that? You might of course do it "imperatively" also.



     public String[] whatWeHave(int user)  {
             String[] s = new String[onHand];
             Node  curr = head;
             int i = 0;
             while(curr != null) {
                  if(data in curr in stock) {
                       s[i] = data;
                       i++;
                  }
                  curr = curr.next();
              }
                    
              return s;
      }



EDIT:: Though, I'm not really sure this is what you want. I have a hard time understanding what int user is supposed to be...

This post has been edited by LaFayette: 27 November 2008 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 kiboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: unable to use LinkedList class - am totally lost.

Posted 27 November 2008 - 02:12 PM

Duh! I hit the wrong reply button :v:

Sorry everyone who read this before!

This post has been edited by kiboko: 27 November 2008 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 kiboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: unable to use LinkedList class - am totally lost.

Posted 27 November 2008 - 02:17 PM

Thank you for the help! I'm going to try it right now.

Sorry, I didn't explain int user well:

This list class is the backend of what will be a GUI (I think that's how you say it - sorry I'm a newbie!)

int user is representative of what the user will enter in. If they enter in 0 - all of the in stock items will be put into the String[] and printed to the GUI...if they enter in 1 - only the in stock items that are books will be put into the the array, etc....if they enter in 6- all of the out of stock items will be placed into an array (which is another method).
Was This Post Helpful? 0
  • +
  • -

#5 kiboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: unable to use LinkedList class - am totally lost.

Posted 27 November 2008 - 04:14 PM

THANK YOU!!! I think that's going to work! I used the "imperative" method...although is the recursive method a better way to do it?

I'm still working on how to integrate the user input (int user)

could I put another if statement in there? or &&?

while (current != null){
		if(user == 0 && current.item.getStatus().equals("i"))  {
				  code for array
		}

}



This post has been edited by kiboko: 27 November 2008 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#6 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: unable to use LinkedList class - am totally lost.

Posted 28 November 2008 - 02:49 AM

the imperative way works just as well.

It's probably best to first check if it's in stock, and then check whether the item is what the users asking for. I'm guessing your using a strings connected to the items to tell what type of product they are. If so it might be better to use a string as the input.

if( !user.equals("outofstock") && current.item.getStatus().equals("i")) {
          //if the input wasnt "outofstock" and it is in stock
          if(user.equals(current.item.getType())
                       s[i] = data;
}
else {
      //if the input is "outofstock" and the item is out of stock.
      if(user.equals("outofstock") && current.item.getStatus().equals("o")) {
             s[i] = data
}
}



if your using ints you might do something similar.

This post has been edited by LaFayette: 28 November 2008 - 03:07 AM

Was This Post Helpful? 0
  • +
  • -

#7 kiboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: unable to use LinkedList class - am totally lost.

Posted 29 November 2008 - 07:13 AM

Thank you LaFayette! You have been a lifesaver!

I do have to keep user as an int, I can't change it to String. So I've been working to implement your suggestions...

I tinkered with this code all day & night yesterday (well, the other methods as well)...and was just about to post that I STILL couldn't get it to work - all of my ararys were null, if I tested all of my equals() statements came back false...when it dawned on me - all of my get() methods have extra formatting to it, so that when the item (as a String in the array) prints to screen it will look like this:

name: price -- status

and I needed to add that to the .equals() as well.

 for example: (if user == 1)
 if(current.item.getName().equals("book -- ")) 



ARGH! I feel like such a MORON!!!! :(

I just tested it for one condition & it seems to work...

Again, thank you for your help! I am hoping these lists will be in a better working condition as soon as I can change everything...and then I can start on the GUI that it runs through - yeay!
Was This Post Helpful? 0
  • +
  • -

#8 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: unable to use LinkedList class - am totally lost.

Posted 29 November 2008 - 04:08 PM

Glad I could help you. Good luck with the rest of the project!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1