import java.util.*;
class TestArrayList {
 public static void main(String args[]) {
  Scanner input = new Scanner(System.in);
  ArrayList al = new ArrayList();
  for (int i = 0; i < 2; i++) {
   System.out.print("Enter food :"); //example: Momo, 2
   String user_input = input.nextLine();
   String formatted_input[] = user_input.split("\\,"); //split into 1 D array
   al.add(formatted_input); //append it to a 2D list
  }
  System.out.println("******************\nMethod 1: to print 2D array list:");
  for (int i = 0; i < al.size(); i++) { //if it is arraylist, we need to use size() instead of .length
   String newarr[] = new String[2];
   newarr = al.get(i); //get subarray in newarr
   System.out.print(newarr[0] + " " + newarr[1] + "\n"); //get 0th and 1th element 
   //if the subarray is large, we need to use another for loop instead of just using 0 and 1
  }
  System.out.println("******************\nMethod 2: to print 2D array list: ");
  for (String[] ob : al) {
   System.out.println(ob[0] + " " + ob[1]);
  }
 }
}
  
All about Computer Science | Linux: OpenSUSE, Red Hat, CentOS, Fedora| RPM Package Manager (RPM) (Red Hat Package Manager)| Programming Concepts | Java | Python | Life Hacks | Microsoft Windows | Tutorials...
Thursday, June 16, 2016
ArrayList Concept in Java [2D ArrayList Java]:
Labels:
2D ArrayList Java,
Java
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment