Using JavaFX, how do I revise this code to use an array rather than an array lis
ID: 3762715 • Letter: U
Question
Using JavaFX, how do I revise this code to use an array rather than an array list?
Here's what I've got but where am I going wrong:
import java.util.Arrays;
public class RevisingGenericStack {
private E[] list;
private int listSize = 1;
public RevisingGenericStack(E[] ilist) {
list = ilist;
listSize = ilist.length;
}
public RevisingGenericStack() {
E[] list = (E[]) Array.newInstance(list.getClass(), 1);
}
public int getSize() { return listSize;
}
public E peek() {
return list[getSize() - 1];
}
public E push(E o) {
listSize++; if(getSize() > list.length) {
listSize = listSize + listSize;
list = (E[]) Array.newInstance(list.getClass(), listSize);
}
list[getSize() - 1] = o; return o;
}
public E pop()
{
listSize--;
E o = list[getSize()];
return o;
}
public boolean isEmpty() {
return (getSize() == 0);
}
}
Here's the Test Program:
import application.RevisingGenericStack;
public class RevisingGenericStackTest {
public static void main(String[] args) {
System.out.println("testGetSize: " + pass(testGetSize()));
System.out.println("testPeek : " + pass(testPeek()));
System.out.println("testPush : " + pass(testPush()));
System.out.println("testPop : " + pass(testPop()));
System.out.println("testIsEmpty: " + pass(testIsEmpty()));
}
public static String pass(boolean b) {
if(b) {
return "Pass";
} else {
return "Fail";
}
}
public static boolean testGetSize() {
RevisingGenericStack<String> stack = new RevisingGenericStack<>();
stack.push("London");
stack.push("Paris");
stack.push("Berlin");
return (stack.getSize() == 3);
}
public static boolean testPeek() {
return false;
}
public static boolean testPush() {
return false;
}
public static boolean testPop() {
return false;
}
public static boolean testIsEmpty() {
return false;
}
}
Help PLS...where am I going wrong?
Explanation / Answer
import java.util.List;
import java.util.ArrayList;
import javafx.collections.ObservableList;
import javafx.collections.ListChangeListener;
import javafx.collections.FXCollections;
public class StackMethods {
private int top;
int size;
int[] stack ;
public StackMethods(int arraySize)
{
size=arraySize;
stack= new int[size];
top=-1;
}
public void push(int value)
{
if(top==size-1)
{
System.out.println("Stack is full, can't push a value");
}
else
{
top=top+1;
stack[top]=value;
}
}
public void pop(){
if(!isEmpty())
top=top-1;
else{
System.out.println("Can't pop...stack is empty");
}
}
public boolean isEmpty(){
return top==-1;
}
public void display(){
for(int i=0;i<=top;i++){
System.out.print(stack[i]+ " ");
}
System.out.println();
}
}
private static void printElements(List<String> list) {
for (Object o : list) {
System.out.println(o.toString());
}
}
}
public class CollectionsDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
ObservableList<String> observableList = FXCollections.observableList(list);
observableList.addListener(new ListChangeListener() {
@Override
public void onChanged(ListChangeListener.Change change) {
System.out.println("Detected a change! ");
}
});
observableList.add("item one");
list.add("item two");
System.out.println("Size: "+observableList.size());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.