How do I implement the toString() method for an ArrayStack?
10,056
You can override toString()
method in ArrayStack
as shown here
. This will solve your problem.
public String toString() {
String result = "";
for (int scan = 0; scan < top; scan++)
result = result + stack[scan].toString() + "\n";
return result;
}
Author by
helloworld
Updated on June 05, 2022Comments
-
helloworld almost 2 years
I want to display a list of orders of type
ArrayQueue <Order>
The classOrder
has anArrayStack<String>
as one of its attributes. I overrode thetoString()
method in the classOrder
, but how do I override it in theArrayStack
class? Because this is the output I get when I display:OrderNumber Name Date ArrayStack@481adc30
What would I have to do to display the Strings in ArrayStack correctly? Do I make changes to class ArrayStack or change something in my Display method?
This is my Display method:
public void display(){ if (!isEmpty()) for (int i = 0; i < numberOfEntries; i++) { System.out.println(queue[(frontIndex + i) % queue.length]); } else System.out.println("You don't have any orders"); }
ArrayStack Class:
public class ArrayStack < T > implements StackInterface < T > { private T [] stack; // array of stack entries private int topIndex; // index of top entry private static final int DEFAULT_INITIAL_CAPACITY = 50; public ArrayStack () { this (DEFAULT_INITIAL_CAPACITY); } // end default constructor public ArrayStack (int initialCapacity) { // the cast is safe because the new array contains null entries @ SuppressWarnings ("unchecked") T [] tempStack = (T []) new Object [initialCapacity]; stack = tempStack; topIndex = -1; } // end constructor /* Implementations of the stack operations */
Order Class:
import java.util.Date; public class Order { int orderNumber; String customerName; Date date; StackInterface <String> items; Order( int number, String name, Date datum, StackInterface<String> item){ orderNumber = number; customerName= name; date= datum; items = item; } /Overriding toString() to Display a list of Orders as one String line. public String toString(){ return orderNumber + " " + customerName + " " + date + " " + items; }
-
helloworld about 9 yearswhere would I add this?
-
Master Mind about 9 yearsyou would add it to your in place of
for (int i = 0; i < numberOfEntries; i++) { System.out.println(queue[(frontIndex + i) % queue.length]); }
-
helloworld about 9 yearsbut that would print nulls if I have only one object in my queue
-
mel3kings about 9 yearsyep otherwise do the printout per Order.
-
helloworld about 9 yearsThat would still print the ArrayStack attribute inside the ArrayQueue as ArrayStack@481adc30 and it will also display nulls if I have only one object in my Queue
-
helloworld about 9 yearsIt didn't fix the ArrayStack@481adc30 problem when I call Display
-
mel3kings about 9 yearsplease put ArrayStack and Order codes in the question as well
-
helloworld about 9 yearsYaay! It kinda worked! but it displayed only the first item in the stack. Why? could it be a mistake in the way I added items to the stack?
int no = input.nextInt(); StackInterface <String> items = new ArrayStack<String>(no); for(int j= 1; j<= no; j++){ System.out.println("Enter item no: " + j ); String theItem = input.next(); items.push(theItem); } Order order1 = new Order (orderno, customername, real_date, items); orders.enqueue(order1);
-
Naman Gala about 9 yearsPlease put it into your question, it is difficult to read code in comments.
-
Naman Gala about 9 yearsShow the
push
method as well asenqueue
method. -
helloworld about 9 yearsNevermind. It worked now! It wasn't printing the last item but I changed
scan < top
toscan<=top
and it works perfect now. Thank you so much! :) -
Naman Gala about 9 yearsYour welcome. Please accept the answer if it helped you.