Question: [JAVA] help ListArrayMain.java - public class ListArrayMain { public static void main (String[] args) { ListArray L1 = new ListArray(); // constructs the list L1
[JAVA] help
ListArrayMain.java
-
public class ListArrayMain {
public static void main (String[] args) {
ListArray L1 = new ListArray(); // constructs the list L1
L1.InsertBegin(2);
L1.InsertBegin(3);
L1.InsertBegin(7);
L1.InsertBegin(15);
L1.InsertBegin(5);
L1.InsertEnd(666);
L1.InsertAfter(1000,7);
// Now, let's print out the array to verify the insertions worked.
System.out.println("The items in the list are:");
for (int i = 0; i
System.out.println(L1.a[i]);
}
System.out.println("The last cell number is: " + L1.lastCell);
}// end main
}
-
ListArray.java
public class ListArray {
//data fields
int a[]; // holds the lists elements
int lastCell; //index of the last item in the list (array);
//cannot be greater than what the constructor allows
// Constructor
public ListArray() {
a = new int[10]; // sets up array of 10 cells, numbered 0 to 9,
//to hold the elements
lastCell = -1; // indicates an empty list
}
// Methods:
public boolean IsFull() {
if(lastCell==9) {
return true;
} else {
return false;
}
}
public boolean IsEmpty() {
/*if(lastCell==-1) {
return true;
} else {
return false;
}*/
return (lastCell==-1);
}
public void InsertBegin(int item) {
if (IsFull() == false) {
for (int i=lastCell;i>=0;i--) {
a[i+1] = a[i];
}
a[0]=item;
lastCell++;
} else {
System.out.println("list is full");
}
}
public void InsertEnd(int item) {
if (IsFull() == false) {
a[lastCell+1] = item;
lastCell++;
}
}
public void InsertAfter(int item, int datum) {
// insert an item after a specifed datum
int locationOfDatum = Find(datum);
// move list items to the right
for (int i=lastCell;i>=locationOfDatum+1;i--) {
a[i+1]=a[i];
}
a[locationOfDatum+1]=item;
lastCell++;
}
public int Find(int item) {
for (int i=0; i
if (a[i]==item) {
return i;
}
};
return -1;
}
}//end class
-
My question:
-
My question:
-
![[JAVA] help ListArrayMain.java - public class ListArrayMain { public static void](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66fa6b12a9bdf_44266fa6b1220663.jpg)
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
