Я пытаюсь создать систему меню «Единый связанный список», используя размер массива 5, в который он позволяет пользователю вставлять элементы.Я создал решение с использованием узлов и ArrayList, но у меня возникли проблемы при создании того же решения с использованием массива.
Решение с использованием узлов:
public class MenuSystem {
public static void main(String[] args) {
SingLinkedList nodeList = new SingLinkedList();
Scanner input = new Scanner(System.in);
boolean done = false;
while(done == false)
{
System.out.println("");
System.out.println("Select an Option");
System.out.println("1. Insert an element at the head");
System.out.println("2. Insert an element at the tail");
System.out.println("3. Insert an element at a position");
System.out.println("4. Delete an element at a position");
System.out.println("5. Check if empty");
System.out.println("6. Get the size of the list");
System.out.println("7. Print the contents of the list");
System.out.println("8. Quit");
System.out.println("");
int selection=input.nextInt();
switch(selection)
{
case 1: //Insert at Front
System.out.println("Enter Data: ");
int frontData = input.nextInt();
nodeList.insertAtFront(frontData);
break;
case 2://Insert at tail
System.out.println("Enter Data: ");
int tailData = input.nextInt();
nodeList.insertAtTail(tailData);
break;
case 3://Insert at position
System.out.println("Select Position: ");
int insertPosition = input.nextInt();
System.out.println("Enter Data: ");
int insertData = input.nextInt();
nodeList.insertAtPosition(insertPosition, insertData);
break;
case 4://Delete at position
System.out.println("Select Position: ");
int deletePosition = input.nextInt();
nodeList.deleteAtPos(deletePosition - 1);
break;
case 5://Check if empty
if(nodeList.isEmpty())
{
System.out.println("List is empty");
}
else
{
System.out.println("List is not empty. Contains "+nodeList.getSize()+" elements.");
}
break;
case 6://Get Size of List
System.out.println("Size is: "+nodeList.getSize());
break;
case 7://Print List
nodeList.printList();
break;
case 8:
System.out.println("The program will now close.");
done=true;
break;
}
}
}
}
Решение с использованием ArrayList:
public class MenuSystem {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
boolean done = false;
while(done == false)
{
System.out.println("");
System.out.println("Select an Option");
System.out.println("1. Insert an element at the head");
System.out.println("2. Insert an element at the tail");
System.out.println("3. Insert an element at a position");
System.out.println("4. Delete an element at a position");
System.out.println("5. Check if empty");
System.out.println("6. Get the size of the list");
System.out.println("7. Print the contents of the list");
System.out.println("8. Quit");
System.out.println("");
int selection=input.nextInt();
switch(selection)
{
case 1: //Insert at Front
System.out.println("Enter Data: ");
int frontData = input.nextInt();
arrayList.add(0, frontData);
break;
case 2://Insert at tail
System.out.println("Enter Data: ");
int tailData = input.nextInt();
arrayList.add(tailData);
break;
case 3://Insert at position
System.out.println("Select Position: ");
int insertPosition = input.nextInt();
System.out.println("Enter Data: ");
int insertData = input.nextInt();
arrayList.set(insertPosition, insertData);
break;
case 4://Delete at position
System.out.println("Select Position: ");
int deletePosition = input.nextInt();
arrayList.remove(deletePosition - 1);
break;
case 5://Check if empty
if(arrayList.isEmpty())
{
System.out.println("List is empty");
}
else
{
System.out.println("List is not empty. Contains "+arrayList.size()+" elements.");
}
break;
case 6://Get Size of List
System.out.println("Size is: " + arrayList.size());
break;
case 7://Print List
for (int i = 0; i < arrayList.size(); i++)
{
int value = arrayList.get(i);
System.out.println(value);
};
break;
case 8:
System.out.println("The program will now close.");
done = true;
break;
}
}
}
}
Я хотел бы построить точно такую же программу, но используя размер массива 5. Я действительно не знаю, как это сделать.Любая помощь будет оценена.