2015-02-04 9 views
6

मैंने एक शोध करने के लिए एक स्टैक और एक ऐरेलिस्ट बनाया। असल में मैं अब अपने स्टैक को एक ऐरेलिस्ट द्वारा प्रतिस्थापित करना चाहता हूं, लेकिन एक स्टैक को एक ऐरेलिस्ट में कैसे परिवर्तित करना है? यह पुश, पॉप ... के साथ कैसे जा रहा है?स्टैक -> ऐरेलिस्ट जावा

धन्यवाद

public static ArrayList<State> search(State finalstate) 
{ 
    ArrayList<State> toreturn = new ArrayList<State>(); 
    Stack<State>mystack=new Stack<State>(); 
    mystack.push(initState); 
    State currState; 
    currState=initState; 
    while(!mystack.isEmpty() && !currState.equals(finalstate)) 
    { 
     currState=mystack.pop(); 
     toreturn.add(currState); 
     if(currState.vecinos.containsKey("up")) 
     { 
      mystack.push(currState).vecinos.get("up"); 
     } 
     if(currState.vecinos.containsKey("down")) 
     { 
      mystack.push(currState).vecinos.get("down"); 
     } 
     if(currState.vecinos.containsKey("left")) 
     { 
      mystack.push(currState).vecinos.get("left"); 
     } 
     if(currState.vecinos.containsKey("right")) 
     { 
      mystack.push(currState).vecinos.get("right"); 
     } 
    } 

    return toreturn; 
} 
+0

खैर 'pop' एक' Stack' से आइटम निकाल देता है, और 'push' आइटम कहते हैं एक 'ढेर' के लिए। 'सूची' पर प्रतिलिपि करते समय आप अपने 'स्टैक' में आइटम क्यों जोड़ रहे हैं? विधि पूर्ण होने पर आप अपनी 'सूची' में क्या चाहते हैं? –

+0

बस एक ट्रैक रखने के लिए सूची – maevy

+0

में इसका पता लगाने के लिए * क्षमा करें – maevy

उत्तर

17

ढेर एक संग्रह है, तो आप उपयोग कर सकते हैं ArrayList (संग्रह) निर्माता

list = new ArrayList(stack); 
संबंधित मुद्दे