0

How do i make a user enter input to eliminate an element from stack A to stack B? For example, i hv 3 stacks: st, st2, st3. I want to let user to choose which stack they want to eliminate an element from and to which stack they want to add that element to. Let say user enter "From: 0" and "To: 1" which means the program hv to remove an element from st and add the removed element to st2. I know one of the ways is somehow like this: 'st2.push(st.pop())' in which will eliminate element from st stack and add to st2 stack. But thats now what i want. I want it to be like this: st(i).push(st(i).pop()) in which "i" indicates the stack position the user entered suppose.

And why does calling hello[0] returns 'bye' only rather than the whole st stack?

package stack; 
import java.util.*;
public class StackDemo {

public static void main(String args[]) {

  // creating stack
  Stack st = new Stack();
  Stack st2 = new Stack();
  Stack st3 = new Stack();

  // populating stack
  st.push("Java");
  st.push("Source");
  st.push("code");
  st2.push("hello");
  st3.push("bye");

  String[] hello = new String[3];
  st.toArray(hello);
  st2.toArray(hello);
  st3.toArray(hello);
  System.out.println("hello: ");
  System.out.println("Get 0: " + hello[0]);
 }
}
  • Put the 3 stacks in an array. – 001 May 26 '18 at 13:14
  • i thought its already in an array String[] hello = new String[3]; st.toArray(hello); st2.toArray(hello); st3.toArray(hello); – norainirahim May 26 '18 at 13:18
  • I think I misread the question. `st(i).push(st(i).pop())` where `i` is some position within the stack - that's not really how stacks work. Stacks are LIFO (last in first out). Maybe you want a different container that allows random access of elements. – 001 May 26 '18 at 13:28
  • so how do i do that? how to make the stacks remove and add based on user's choice of stack? – norainirahim May 26 '18 at 13:32
  • You would do that by putting the stacks into an array: `Stack[] stacks = new Stack[3];` instead of `st, st2, st3`. – 001 May 26 '18 at 13:35

2 Answers2

0

Calling toArray does not append the values to the array. This is from the javadoc of Vector.toArray

Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array. If the Vector fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this Vector.

st.toArray(hello); //Now, the array contains [Java, Source, code]
st2.toArray(hello); //the array is now [hello, null, code]
st3.toArray(hello);//the array is now [bye, null, code]

The reason for adding null is clearly added in the javadoc

If the Vector fits in the specified array with room to spare (i.e., the array has more elements than the Vector), the element in the array immediately following the end of the Vector is set to null.

To copy the contents of the three stacks into an array, you could do

String[] myHello = new String[st.size() + st2.size() + st3.size()];
System.arraycopy(st.toArray(), 0, myHello, 0, st.size());
System.arraycopy(st2.toArray(), 0, myHello, st.size(), st2.size());
System.arraycopy(st3.toArray(), 0, myHello, st.size() + st2.size(), st3.size());

System.out.println(Arrays.toString(myHello)); //[Java, Source, code, hello, bye]

Note, that since you are using a raw type, calling st.toArray() returns a Object[] and not a String[]. So, each of the copy statements issues a warning.

See What is a raw type and why shouldn't we use it?

EDIT: Your question and the attempt showed different intents. The above will help you to get the elements of the Stack into one array. If you need to store the stacks itself in the array, you need to declare Stack [] stacks = new Stack[3] and

stacks[0] = st;
stacks[1] = st2;
stacks[2] = st3;

With this you can do stacks[i].push(stacks[i].pop());

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
0

toArray(arrName) function returns the content of whole list in a form of array, given the type of elements in list matches with the type of array arrName.

It doesn't store the reference to the list itself. I repeat, it only stores the elements in the list given the types match. Otherwise it throws exception. ArrayStoreException is thrown in case of type mismatch.

For the first part of your question, I would say maintain an ArrayList of type Stack and add the Stack objects in that. Then you can access the reference to Stack objects by using get(index) instance method of ArrayList.

ArrayList<Stack> at = new ArrayList<>();
at.add(st);
at.get(0); //returns a reference to st
Kushal Mondal
  • 93
  • 1
  • 8