将数组转换为通用数组

时间:2016-08-08 03:28:20

标签: java arrays generics

尝试将旧程序的数组转换为通用数组。这是我第一次使用仿制药,我在这里显然遗漏了一些东西。我试图将一个字符串的字符推入一些专用堆栈时得到一个NPE。我打印了在evaluateExpression方法中初始化的数组的长度,得到0.

我如何开始这种初始化?我的构造函数不适合设置每个数组的容量吗?我不是主要在适当的范围内调用东西吗?

我已经在代码中指出我在第33行获得了我的第一个NPE。其他评论和其他程序可能无关紧要。我不关心程序是否像它应该的那样运行,只是这些通用数组。

<table>
  <tr>
    <th>
      Column 1
  </th>
  <th>
    Column 2
    </th>
  <th>
    Column 3
    </th>
    </tr>
<tr>
  <td>
    1
    </td>
  <td>
    Apples
    </td>
  <td>
    Red
    </td>
  </tr>
<tr>
  <td>
    2
    </td>
  <td id="removeMe">
    Pineapple
    </td>
  <td>
    Yellow
    </td>
  </table>
<button onclick="removeTheCell()">Remove Pineapple</button>

数组构造函数,推送和弹出函数:

package generics;

import java.util.Scanner; 

public class GenericTesting<E> {

/**
 * function converts input expression to a char array, 
 * which is then parsed and its elements pushed into int and char arrays
 * while spaces are collected and separated from what will be equated
 * @param expression prefix expression entered by user
 */
public static int evaluateExpression(String expression) {
    int result = 0; 

    char[] tokens = expression.toCharArray();
    StackG<Integer> operands = new StackG<>(10); 
    StackG<Character> operators = new StackG<>(10); 
    StackG<Character> blanks = new StackG<>(10);

    for (int i = tokens.length -1; i > 0; i--) {
        if (tokens[i] == ' ') 
            blanks.push(tokens[i]);
        if (tokens[i] >= '0' && tokens[i] <= '9') 
            operands.push(tokens[i]); 
        if (tokens[i] == '*' || tokens[i] == '+' || tokens[i] == '-')
            operators.push(tokens[i]); 
    }

    while (operands.getLength() != 1) {
        int num1 = operands.pop();     // null pointer exception
        int num2 = operands.pop(); 

        char symbol = operators.pop(); 
        if (symbol == '*')
            result = num1 * num2; 
        if (symbol == '+');
            result = num1 + num2; 
        if (symbol == '-')
            if (num1 > num2)
                result = num1 - num2; 
            else 
                result = num2 - num1; 
    }
    return result; 
}

客户代码:

public class StackG<E>{
    private E[] stack;     // holds stack elements 
    private E[] stack2;    // holds stack elements while stack is resized
    private int capacity;  // number of elements that can be stored
    private int length;    // number of currently stored elements
    private static int top;       // stack top pointer

public StackG(int initialCapacity) {
    capacity = initialCapacity; 
    stack = (E[]) new Object[capacity];  
    length = 0; 
    top = 0;
}

public StackG() {
    capacity = 5; 
    stack = (E[]) new Object[capacity]; 
    length = 0; 
    top = 0; 
}

public void push(Object item) {
    if (top == stack.length)
        throw new IllegalArgumentException(); //Stack is full
    else {
        stack[top] = (E) item; 
        top++;
        length++;
    }
}

public E pop() {
    if (isEmpty())
        throw new IllegalArgumentException(); //Stack is empty
    else {
        top--;
        length--;
        return stack[top]; 
    } 
}

0 个答案:

没有答案