In: Computer Science
Input:
Linked2DArray.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;
public class Linked2DArray { //Define a class
LinkedList<String> list; //Declare a member variable
Linked2DArray(){
list = new LinkedList<>(); //Initialize linkedlist
}
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("input.txt")); //Initialize scanner with file
Linked2DArray arr = new Linked2DArray(); //Create an object of class
while (scanner.hasNextLine()) //Read file one line at a time
{
Scanner inner_scanner = new Scanner(scanner.nextLine()); //Parse the line and split by whitespaces
while (inner_scanner.hasNext()) //Read until the end of line
{
arr.list.add(inner_scanner.next());
}
arr.list.add("x"); //Add an indicator for next row
}
for (String s: arr.list) //Read elements of linkedlist
{
if (s.equals("x")) //If indicator found, switch to next line
{
System.out.print("\n");
continue;
}
System.out.print(s+" "); //Print elements
}
}
}
Output:

Brief Explanation: