Difference between Array and ArrayList in Java

 Program using Array:

public static void main(String[] args) { Reader r = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(r); // Read strings from the keyboard String[] list = new String[10]; for (int i = 0; i < list.length; i++) { String s = reader.readLine(); list[i] = s; } // Display the contents of the array for (int i = 0; i < list.length; i++) { int j = list.length - i - 1; System.out.println( list[j] ); } }


Program using ArrayList:

public static void main(String[] args)

{ Reader r = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(r); // Read strings from the keyboard ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < 10; i++) { String s = reader.readLine(); list.add(s); } // Display the contents of the collection for (int i = 0; i < list.size(); i++) { int j = list.size() - i - 1; System.out.println( list.get(j) ); }

Comments

Popular posts from this blog

Java Program to create a large array and copy its values equally to two small arrays and display the second array only

Python program to sum up the Salary when the Name and Age matches with subsequent rows.