Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

What if I want to copy a stack to an array. (the order has to be reversed)?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-v3-0324’s answer.
  1. User

    What if I want to copy a stack to an array. (the order has to be reversed)?

  2. Assistant

    If you want to copy a Stack to an array in reversed order (i.e., the element at the top of the stack should be the first element in the array), here are some ways to do it in Java:


    1. Using Stack and manual reverse copy

    Since Stack extends Vector, you can access elements by index. The top of the stack is at the end of the list (stack.size() - 1), so to reverse, copy elements starting from the top down to the bottom.

    import java.util.Stack;
    
    public class Main {
        public static void main(String[] args) {
            Stack<String> stack = new Stack<>();
            stack.push("A");
            stack.push("B");
            stack.push("C");  // Top of stack
    
            String[] array = new String[stack.size()];
            int j = 0;
            for (int i = stack.size() - 1; i >= 0; i--) {
                array[j++] = stack.get(i);
            }
    
            // Print reversed array
            for (String s : array) {
                System.out.println(s);
            }
        }
    }
    

    Output:

    C
    B
    A
    

    2. Using Streams (Java 8+) with Collections.reverse()

    You can create a copy of the stack as a list, reverse it, then convert to an array:

    import java.util.Collections;
    import java.util.List;
    import java.util.Stack;
    
    public class Main {
        public static void main(String[] args) {
            Stack<String> stack = new Stack<>();
            stack.push("A");
            stack.push("B");
            stack.push("C");
    
            // Copy stack to a list
            List<String> list = List.copyOf(stack); // Immutable copy (Java 10+)
            // Or: List<String> list = new ArrayList<>(stack);
    
            // Reverse the list
            Collections.reverse(list);
    
            // Convert to array
            String[] array = list.toArray(new String[0]);
    
            for (String s : array) {
                System.out.println(s);
            }
        }
    }
    

    3. Using Streams with IntStream to reverse indices

    If you want a one-liner with streams:

    import java.util.Stack;
    import java.util.stream.IntStream;
    
    public class Main {
        public static void main(String[] args) {
            Stack<String> stack = new Stack<>();
            stack.push("A");
            stack.push("B");
            stack.push("C");
    
            String[] array = IntStream.range(0, stack.size())
                .mapToObj(i -> stack.get(stack.size() - 1 - i))
                .toArray(String[]::new);
    
            for (String s : array) {
                System.out.println(s);
            }
        }
    }
    

    Summary:

    • The top of the stack is the last element in the underlying list.
    • To reverse, copy elements starting from the last index down to zero.
    • Use either a simple loop, Collections.reverse(), or streams with reversed indices.

    If you want, I can help you with a specific example or optimize for your use case!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.