Comparing 2 custom type arrays

Viewed 31

How do I compare 2 arrays with generic type. It's my main file with stack and it's function, I also pressed alt+insert and made new equals method

import java.util.Arrays;

public class Stack<T> {

  int top;
  T[] stack;

  public Stack() {
    stack = (T[]) new Object[2];
    top = 0;
  }

  public void push(T element) {
    if ((top+1) == stack.length) {
      stack = Arrays.copyOf(stack, top + 2);
    }
    stack[++top] = element;
  }

  public void pop() {
    if (top == 0){
      System.out.print("Stack is already empty");
    }
    stack[top--] = null;
  }
  public int count() {
    return top;
  }

  @Override
  public int hashCode() {
    return super.hashCode();
  }

  @Override
  public boolean equals(Object obj) {
    return super.equals(obj);
  }
}

And it's my file with tests

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class StackTests {
  @Test
  public void example() {
    Stack<Integer> actual = new Stack<>();
    actual.push(6);
    Stack<Integer> actual1 = new Stack<>();
    actual1.push(6);
    Assertions.assertEquals(actual,actual1);
  }
}

I am trying to compare two equal arrays with Assertions.assertEquals and new equals method, but it's wrong. In debug I see that my arrays have identical elements. Are there any ways to fix this? I'm new in Java, help me please

0 Answers
Related