我想知道如何扭转在java中 我已经做了简单的部分用户inputed字或字符串..使用堆栈结构扭转一个字符串/字
import java.util.Scanner;
public class Stack
{
public static void main(String args[]){
System.out.println("Please enter a word to reverse: ");
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
}
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack
public Stack(int s) { // constructor
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
}
public void push(long j) { // put item on top of stack
top++;
stackArray[top] = j; // increment top, insert item
}
public long pop() { // take item from top of stack
return stackArray[top--]; //access item, decrement top
}
}
这基本上只是发生在用户输入和什么也不做(并有pop和push方法)
只是想知道,如果有人可以帮助我扭转串让我们说“你好”(记住其用户提交的字符串不过)
任何帮助将不胜感激
那么,将单词中的每个字符推入堆栈时会发生什么? – Kayaman 2014-11-24 20:24:18
你快到了!将“Hello”插入堆栈 - >'[H,e,l,l,o - >'。弹出堆栈中的每个元素,首先弹出“o”,然后是l,l,e,最后是H. – Maroun 2014-11-24 20:25:11
您是否需要使用堆栈,因为您可以使用StringBuilder.reverse ... – brso05 2014-11-24 20:25:55