Skip to main content

ARRAY vs ARRAYLIST


ARRAY vs ARRAYLIST
Array is an object used to save similar data type elements and its size is limited.
Array is static in nature. Once created we cannot change size of array.
  • ·       Random access, linear data structure
  • ·       Fixed size once created
  • ·       Can contain objects and primitives
  • ·       Must declare element type
  • ·       It is not a class
  • ·       Elements accessible with index number
Eg: int[] ar=new int[3]; //stores complete integer types

Arraylist is a part of Collection Framework of util package to store different data type objects and it is growable. Arraylist is dynamic in nature. It can be re-size itself when needed depending upon capacity and load factor.
  • ·       Random access, linear data structure
  • ·       Dynamic size, grows automatically
  • ·       Can only contain objects
  • ·       Element type  is OBJECT
  • ·       It is a class with many methods
  • ·       Accessing methods like get() etc are available
Eg: Arraylist arr=new Arraylist();
       arr.add(13); //integer type
       arr.add(13); //string type

ArrayList<String> stringList = new ArrayList<String>(); //Generic ArrayList to Store only String objects

The Methods Of ArrayList class are:
1)Add - stringList.add(“item”);
2)Remove- stringList.remove(0);
3)Clear- stingList.clear();
4)Insert
5)TrimToSize
6)Sort
7)Reverse 

Example:
import java.util.ArrayList;
public class Program
{
          public static void main(String[] args)
          {
// Create new ArrayList.
                              ArrayList<Integer> elements = new ArrayList<>();
// Add three elements.
                              elements.add(10);
                              elements.add(15);
                              elements.add(20);
// Get size and display.
                              int count = elements.size();
                              System.out.println("Count: " + count);
// Loop through elements.
                              for (int i = 0; i < elements.size(); i++)
{
                                          int value = elements.get(i);
                                          System.out.println("Element: " + value);
                              }
          }
}

Output
Count: 3
Element: 10
Element: 15
Element: 20

Comments

Popular posts from this blog

Simulation Practice Problems

1. There is only one telephone in a public booth of a metro station. Following table indicates the distribution of arrival time and duration of calls: Time Between arrivals (minutes) 4 5 6 Probability 0.1 .6 0.3 Call Duration (minutes) 3 4 5 6 Probability 0.14 0.6 0.12 0.14 Simulate for 100/500/1000 times. Conduct 3 such trials. It is proposed to add another telephone to the booth. Justify the proposal based on the waiting time of caller.Generate graphs to support your answer. 2. A baker needs to find out the cookies he need to bake each day. The probability distribution for the cookies customers is as follows: Number of customers/day 7 9 11 14 Probability 0.35 0.30 0.15 0.20 C...

System Analysis and Design (SAD)

Introduction to System Analysis and Design (SAD) System are created to solve Problems. One can think of the systemsapproch as an organised way of dealing with a problem. In this dynamic world , the subject system analysis and design, mainly deals with the software development activities. This post include:- What is System? What are diffrent Phases of System Development Life Cycle? What are the component of system analysis? What are the component of system designing? What is System? A collection of components that work together to realize some objectives forms a system. Basically there are three major components in every system, namely input, processing and output. In a system the different components are connected with each other and they are interdependent. For example, human body represents a complete natural system. We are also bound by many national systems such as political system, economic system, educational system and so forth. The objective of the system demands tha...
Advantages and Disadvantages of EIS Advantages of EIS Easy for upper-level executives to use, extensive computer experience is not required in operations Provides timely delivery of company summary information Information that is provided is better understood Filters data for management Improves to tracking information Offers efficiency to decision makers Disadvantages of EIS System dependent Limited functionality, by design Information overload for some managers Benefits hard to quantify High implementation costs System may become slow, large, and hard to manage Need good internal processes for data management May lead to less reliable and less secure data