java 动态求某个数组栈先进后出的结果
public class DemoZhan {
static String[ ] arr = new String [ ] { "1" , "2" , "3" , "4" , "5" } ;
static int i= 0 ;
public static void main ( String[ ] args) {
LinkedList< String> link = new LinkedList < String> ( ) ;
LinkedList< String> result = new LinkedList < String> ( ) ;
for ( String item : arr) {
link. add ( item) ;
}
inputArr ( link, result, "" ) ;
}
public static void inputArr ( LinkedList< String> link, LinkedList< String> result, String out) {
if ( result. size ( ) > 0 ) {
out= out + result. pollFirst ( ) ;
}
if ( out. length ( ) == arr. length) {
i++ ;
System. out. println ( "第 " + i+ "个输出结果为: " + out) ;
return ;
}
int size = link. size ( ) ;
if ( result. size ( ) > 0 ) {
LinkedList< String> link2 = new LinkedList < String> ( ) ;
LinkedList< String> result2 = new LinkedList < String> ( ) ;
if ( size> 0 ) {
link2. addAll ( link) ;
}
result2. addAll ( result) ;
inputArr ( link2, result2, out) ;
}
for ( int i = 0 ; i < size; i++ ) {
result. push ( link. pollFirst ( ) ) ;
LinkedList< String> link3 = new LinkedList < String> ( ) ;
LinkedList< String> result3 = new LinkedList < String> ( ) ;
link3. addAll ( link) ;
result3. addAll ( result) ;
inputArr ( link3, result3, out) ;
}
}
}