Burada ve programlamada yeniyim. Eğitmenim yeterli bir yardım olmadığı için tek başıma başka konuları incelemeye çalışıyorum. Genel bir Stack ile bir kelimeyi tersine çevirmek istiyorum.Yığınla Bir Kelimeyi Tersine Çevirme
Benim pop, push, isEmpty ve peek yöntemleri işe yarar (bunları daha önce yaptığım daha basit bir programla test ettim.) Ve çıktı bana tersine çevirilen karakterleri char tarafından her zaman veriyormuş gibi görünüyor. her char önce bana bir null veriyor!
Sorularım: Bu neden oluyor? Kapasite 9 olduğunda çalışmak için bir expandCapacity yöntemim olsa da, ancak giriş sınırı geçtiğinde geçerli değildir.
İşte İşte benim kod
package Stack;
import java.util.Scanner;
public class ReverseDriver<T> {
private static String out;
private static String in;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your sentence: ");
in = input.nextLine();
int size = in.length();
ArrayStack<Character> revStack = new ArrayStack<>(size);
for (int i = 0; i < in.length(); i++) {
char u = in.charAt(i);
revStack.Push(u);
if (in.length() > 9) {
revStack.expandCapacity();
}
}
while (!revStack.IsEmpty()) {
char u = revStack.Pop();
out = out + u;
System.out.flush();
System.out.print(out);
}
}
}
var Çıktı
run:
Enter a word:
word
nullr
nullro
nullrow
Exception in thread "main" java.lang.NullPointerException
at Stack.ReverseDriver.main(ReverseDriver.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
DÜZENLEME var: Burada ben çalıştığını söyledi yöntemleri var. out
içinde
@Override
public void Push (T element)
{
if (count == stack.length){
expandCapacity();
}
stack[++count] = element;
//System.out.println(count);
}
@Override
public String toString()
{
String result = "<top of stack>\n";
for (int index=count-1; index >= 0; index--){
result += stack[index] + "\n";
}
return result + "<bottom of stack>";
}
@Override
public boolean IsEmpty()
{ //Checks if array is empty
if(count == 0){
System.out.println("Nothing");
}
return count == 0;
}
public T Pop()
{
T output;
output = (stack[count - 1]);
count--;
return(output);
}
@Override
public T Peek()
{
//looks at the object at the top of this stack without removing it
//from the stack.
if(stack.length == 0){
// {
System.out.println("Cant peek a ghost");
}
return(stack[--count]);
}
// else
// {
// System.out.println(stack[count-1]);
// }
// }
@Override
public int Size()
{
//Sets the size of this vector
if(stack.length == 0){
System.out.println("Nothing inside");
}
System.out.println("The array's size is : " + count);
return count;
}
}
ben takdir ederken bunu sadece bize bu yüzden içine biraz daha fazla fikir vermek için, bize expandCapacity ve isEmpty yönteminde kod veriniz olabilir, çalışıyor söyledi oluyor :) – christopher
Bunun çıktısını üreten aynı kod olduğundan emin misiniz - "Enter a word" vs "Bir cümle girin" –
not: 'expand.Capacity 'in.length()' 10 ise 10 kez çağrılır kasıtlı? Ayrıca, '' '' '' '' '' '' '' '' '' ifadesi, statik olarak değil, en azından yerel değişkenler olmalıdır (en azından bu bir "gerçek" program ise). – hyde