[JAVA] help My question: ——————-————————————————— 1. Do the following two tasks.
ID: 3754033 • Letter: #
Question
[JAVA] help
My question:
——————-—————————————————
1. Do the following two tasks. Submit the code in ListArrayMain.java that does the following: . In ListArrayMain, construct an array and create the array which is the reversal of the given array and print it out. In ListArrayMain, construct two arrays of the same size and produce an array that "interlaces" them, and prints out the interlaced array. Example if array a consists of data 2, 5, 3, 6 and array b consists of 12,7, 4, 2, then the interlaced array should be 2, 12, 5, 7, 3, 4, 6, 2Explanation / Answer
Below is the updated ListArrayMain class which includes solution for both problems.
---------------------------
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);
// Task1
System.out.println("---Task1---");
// 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 <= L1.lastCell; i++) {
System.out.println(L1.a[i]);
}
System.out.println("The last cell number is: " + L1.lastCell);
ListArray reverseArray = new ListArray();
for (int i = L1.lastCell; i >= 0; i--) {
if (i == L1.lastCell) {
reverseArray.InsertBegin(L1.a[i]);
} else if (i == 0) {
reverseArray.InsertEnd(L1.a[i]);
} else {
reverseArray.InsertAfter(L1.a[i], L1.a[i + 1]);
}
}
System.out.println("The items in the reverse list of ListArray L1 are:");
for (int i = 0; i <= reverseArray.lastCell; i++) {
System.out.println(reverseArray.a[i]);
}
// Task2- Interlaced Array
System.out.println("---Task2---");
ListArray L2 = new ListArray(); // constructs the list L2
L2.InsertBegin(2);
L2.InsertAfter(5, 2);
L2.InsertAfter(3, 5);
L2.InsertEnd(6);
System.out.println("The items in the list L2 are:");
for (int i = 0; i <= L2.lastCell; i++) {
System.out.println(L2.a[i]);
}
ListArray L3 = new ListArray(); // constructs the list L3
L3.InsertBegin(12);
L3.InsertAfter(7, 12);
L3.InsertAfter(4, 7);
L3.InsertEnd(2);
System.out.println("The items in the list L3 are:");
for (int i = 0; i <= L3.lastCell; i++) {
System.out.println(L3.a[i]);
}
ListArray interlacedListArray = new ListArray();
for (int i = 0; i <= L2.lastCell; i++) {
if (i == 0) {
interlacedListArray.InsertBegin(L2.a[i]);
interlacedListArray.InsertAfter(L3.a[i], L2.a[i]);
}else if (i == L2.lastCell) {
interlacedListArray.InsertAfter(L2.a[i], L3.a[i - 1]);
interlacedListArray.InsertEnd(L3.a[i]);
} else {
interlacedListArray.InsertAfter(L2.a[i], L3.a[i - 1]);
interlacedListArray.InsertAfter(L3.a[i], L2.a[i]);
}
}
System.out.println("Interlaced Array of ListArrays L2 and L3");
for (int i = 0; i <= interlacedListArray.lastCell; i++) {
System.out.println(interlacedListArray.a[i]);
}
}// end main
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.