Wednesday, December 3, 2014

JAVA ArrayList

JAVA ARRAYLIST

April 2013.  In preparation for going back to school under the auspices of the veteran's VRAP program, I perused more computer books.  I studied, in particular, a small paperback computer dictionary, from the front cover to the back cover.  I read every computer terminologies that were in the dictionary and their definitions.  Not all the sentences, but at least the first sentence.  I did this because I did not really know what computer knowledge was expected from a programming student.

I also read encyclopedias on Computer Science because it may help me further understand learning computer programming.  As the old adage goes, "Chance favors the prepared mind".

While we're on the subject of dictionaries, here's a sample program for writing dictionaries.

/*
   Sample program illustrating the use of ArrayList class.
   Program for a dictionary of three terms and definitions.
*/

import java.util.*;

public class ArrayListSample
{
  public static void main(String[] args)
  {
    ArrayList<String> term = new ArrayList<String>();
    ArrayList<String> definition = new ArrayList<String>();
    Scanner scanner = new Scanner(System.in);
    for(int i = 0; i < 3; i++)
    {
      System.out.print("Enter term: ");
      term.add(scanner.nextLine());
      System.out.print("Enter definition: ");
      definition.add(scanner.nextLine());
    }
    scanner.close();
    System.out.println("\tLIST OF TERMS & DEFINITIONS");
    for(int i = 0; i < 3; i++)
    {
      System.out.print(term.get(i) + ": ");
      System.out.println(definition.get(i));          
    }
  }
}

Here is a sample compilation and execution of this code:

C:\>javac ArrayListSample.java

C:\>java ArrayListSample
Enter term: computer
Enter definition: device programmer uses.
Enter term: programmer
Enter definition: person that writes game software, etc.
Enter term: C
Enter definition: programming language and letter of alphabet.
        LIST OF TERMS & DEFINITIONS
computer: device programmer uses.
programmer: person that writes game software, etc.
C: programming language and letter of alphabet.

This program uses parallel arrays while implementing the ArrayList class.  It only has three elements, but you can have as many elements as you want providing your computer has enough memory for all these elements.

If you plan to have a million elements in your ArrayList, or even close to ten thousand elements, depending on memory requirements, you should opt for a better technology to improve your system and software performance.  A program that implements linked list written in C or C++ can make your software go faster.  And if looking for a term, implement a binary search algorithm for faster searching.

Happy computing!

John Sindayen

1 comment:

  1. JAVA ArrayList is a important thing in collections in JAVA which is used to store internally and also in a dynamic way in Online Java Course

    Java Online Training

    ReplyDelete