Array-Based List questions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Array-Based List questions

  1. #1
    Join Date
    Feb 2006
    Posts
    3

    Array-Based List questions

    Hello, I have a question on how array-based lists can be used in a linked list setting. My Java teacher wasn't very clear on how it works so I'm hoping if anyone has explain it to me or direct me to a site where they might be able to I would be most appreciative.

    The example involved three arrays and a list.

    String[] names = {"jeans","shirt","boots"};
    double [] costs = {39.99, 24.75, 105.00};
    int[] stores = {16, 29, 3};
    MallList listOne = new MallList{names, costs, stores};

    While there was a base class and a derived class i'm digressing. Can anyone either explain how the arrays can be put into the list or direct me to a site where they might start with maybe just one array.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Your declaration of MallList should have parentheses around the names of the arrays, not brackets ... new MallList( names, costs, stores );

    I interpret your description as requiring that the MallList class is supposed to take the data in the arrays which are passed as parameters and hold that data in a linked list. Please correct me if that interpretation is wrong.

    These three parallel arrays hold the information which, when put together, describes each store of the mall. You refer to the combination of information by the index they share.

    If the MallList is supposed to be a linked list, and linked lists hold data and a link, you can create a Node class for your list which holds, as data, one tuple of data indexed by "0", another by "1", and another by "2" from names, costs, stores. It also holds a link to the next Node.

    So, node0.name = name[0], node0.cost = costs[0], node0.store = stores[0].

    Iterate through your arrays, populating the current node with the values stored there, and add the resulting nodes, in turn, to the list.

  3. #3
    Join Date
    Feb 2006
    Posts
    3
    Thank for the help. I did gain a better understanding, unfortunately nodes were not required for this program. I'm hoping if I put up the code I have so far my question would be better understood. I do apologize for the format in advance as well.




    //This program will take 3 arrays and make them into a list
    //Depending of the type of item, its cost, and its store number
    import java.io.*;
    public class TestListStudent
    {
    public static void main(String[] args)
    {
    String[] names = {"jeans", "shirt", "boots"}; //array for items
    double[] costs = {39.99, 24.75, 105.00}; //array for their costs
    int[] stores = {16, 29, 3}; //store number that they are at
    MallList listOne = new MallList(names,costs,stores);
    System.out.println("Is the list full" + listOne.isListFull());//check to see if list is full
    listOne.printFowards(); //Print the list
    listOne.Insert("socks" + 5.64 + 80); //add socks and its variables to the list
    System.out.println("The list in reverse");
    listOne.printBackwards(); //Print the list backwards
    System.out.println("Is the list full" + listOne.isListFull());//check to see if list is full again
    listOne.Insert("necklace" + 12.5 + 3); //add another item
    listOne.printFowards(); //print again
    System.out.println("Is the list full" + listOne.isListFull());//is list full
    }
    }


    public class ItemList //Class that holds methods to print the list
    {
    public int listOne = new MallList[maxSize];
    public int maxSize = 5;
    public int length = 0;
    public ItemList(int maxS, int length, int[] MallList)
    {
    listOne = MallList[];
    TotalLength = length;
    maxSize = maxS;
    }
    public boolean isListFull() //checks to see if the list is full
    {
    return (TotalLength == maxSize);
    }
    public void printBackwards(int listOne[]) //method to print list backwards by recursion
    {
    if(int i <= TotalLength)
    {
    System.out.println(listOne[i]);
    i--
    backwards(i);
    }
    }
    public void Insert(int Item) //method to put an item into the list
    {
    int length = 0;
    int maximum = 5;
    int Item = 0;

    if(length == 0)
    listOne[length++] = insertItem;
    else
    if(length == maximum)
    System.out.println("The list is full");
    else
    listOne[length++] = Item;
    }
    public void printFowards(int listOne[]) //method to print list fowards
    {
    for(int i = 0; i < TotalLength; i++)
    System.out.println(listOne[i]);
    }
    }


    public class Item //Base class
    {
    private String type;
    private int cost;

    public Item(String itemType, int amount) //constructor with type of item and amount it costs
    {
    type = itemType;
    cost = amount;
    }

    public void List() //Prints the list
    {
    super(List);
    System.out.print(type + " " + cost);
    }
    }



    public class MallItem extends Item //Derived class from Item
    {
    private int Store;

    public MallItem(int storeNumber)//Constructor
    {
    Store = storeNumber;
    }

    public void Printitems() //Prints the store number with respect to the list
    {
    System.out.println(" "+Store);
    }
    }

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I think you need to put some time into organizing your project. It is time to put pencil to paper and chart out what class structure you are creating, what methods and data in each, and the interaction of classes. What is your program supposed to be doing?

    What does your MallList class look like?

    In ItemList you seem to declare the object "listOne" (which has all sorts of methods being called in your method main) as an int but then define it as an array of MallList elements.

    Why would you assign values to data members of your ItemList class and then have a constructor which takes arguments which redefines these data members?

    What is the List() method supposed to be doing in your Item class? (remember the naming pattern - methods are supposed to start with a lower case letter). What is the call to super(List) supposed to do?

    I don't understand the relationship of Item and MallItem: can you explain it to me? It does not make sense to me that one derives from the other. You make no apparent use of the inherited data members in the child class.

Similar Threads

  1. Breadth first search with java
    By Ant_Magma in forum Java
    Replies: 26
    Last Post: 03-07-2006, 09:12 AM
  2. Replies: 2
    Last Post: 04-15-2005, 10:06 PM
  3. Problem with linked lists and iterators
    By white94cam in forum C++
    Replies: 2
    Last Post: 03-31-2005, 12:02 AM
  4. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 05:21 PM
  5. HOW CAN I CREATE ARRAY IN SQL 6.5
    By Toms Ng in forum Database
    Replies: 1
    Last Post: 12-18-2001, 10:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center