Theory – IV

Container class

Stores a collection of other objects and provide sequential or direct access to them. The string class is a container that holds chars. All container classes access the contained elements safely and efficiently by using iterators. Container class is a class that hold group of same or mixed objects in memory.

Containers of pointers provide containers to hold the objects that are heap-allocated in manner that is exception-safe and with minimum overhead.

Types

  • Sequence containers: array, vector, list
  • Container adaptors: stack, queue, priority_queue
  • Associative containers: set, map

Functions that:

  • Create an empty container (via a constructor)
  • Insert a new object into the container
  • Remove an object from the container
  • Report the number of objects currently in the container
  • Empty the container of all objects
  • Provide access to the stored objects
  • Sort the elements (optional)

Example:

Creating containers:  vector<int> numbers(7);
Extending a container:  vector<int> v; // creates an empty vector
                        v.push_back(3);
                        v.push_back(8);

Accessing elements of a container:   v[1]=3;
                                     v[2]=5;
                                     v[3]=v[1]+v[2];


find - looks for a value
vector<int>::iterator it;
  ...
  it = find (v.begin(), v.end(), 30);
  if (it ==  v.end())
    cout << "30 not found " << endl;
  else
    cout << "30 is in v " << endl;

count
vector<int> v;
cout << "30 is in v "  << count(v.begin(), v.end(), 30) << " times" << endl;

for_each
    void square(int n) {
       cout << n*n << endl;
    }

    vector<int> v;

    // print the square of all the values in v 
    for_each(v.begin(), v.end(), square);

transform
    int square(int n) {
       return  n*n;
    }

    vector<int> v;

    // replace each value in v by its square
    transform(v.begin(), v.end(), square);

Replace
replace(v.begin(), v.end(), 7,3);

fill 
    fill(v,9); 

copy
 vector<int> v(10);
for (int i=0;i<v.size();i++)
  v[i]=i;
list<int> l(10);
copy(v.begin(), v.end(),l.begin());

remove
it=remove (v.begin(), v.end(), 7);

min_element
it=min_element(v.begin(), v.end());

Adapter class

Provides an empty implementation of all methods in an event listener interface i.e this class itself write definition for methods which are present in particular event listener interface. However these definitions does not affect program flow or meaning at all.

useful when you want to receive and process only some of the events that are handled by a particular event listener interface.

Suppose you want to use MouseClicked Event or method from MouseListener, if you do not use adapter class then unnecessarily you have to define all other methods from MouseListener such as MouseReleased, MousePressed etc.

But If you use adapter class then you can only define  MouseClicked method and don’t worry about other method definition because class provides an empty implementation of all methods in an event listener interface.

xyz

Example:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="MainClass" height="800" width = "500">
</applet>
*/

public class MainClass extends Applet 
{
     public void init()                   
     {
           addMouseListener(new AdapterTest(this));
      }
}   

// AdapterTest Created
class AdapterTest extends MouseAdapter  
{
       MainClass MainClassObj;  
       public AdapterTest(MainClass MainClassObj) 
       {
              this.MainClassObj=MainClassObj;           
        } 

        public void mouseClicked(MouseEvent me)
        {
                 MainClassObj.showStatus("Mouse Has Been Clicked ");
         }
}

Memory space for a program has two parts:

  • Data segment

A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. The size of this segment is determined by the values placed there by the programmer before the program was compiled or assembled, and does not change at run-time. The data segment is read-write, since the values of the variables can be altered at run-time.

Example – Stack, Heap, uninitialized and initialized data

  • Code segment or Text segment

It is one of the sections of a program in an object file or in memory, which contains executable instructions. It has a fixed size and is usually read-only. If the text section is not read-only, then the particular architecture allows self-modifying code.

Example – main()

xyz

Finalize method

Before an object is garbage collected, the runtime system calls its finalize() method. The intent is for finalize() to release system resources such as open files or open sockets before object gets collected.

By using finalization, you can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector. It is not called when an object goes out-of-scope.

To add a finalizer to a class, you simply define the finalize() method. The java run time calls that method whenever it is about to recycle an object of that class. Inside the finalize() method you will specify those actions that must be performed before an object is destroyed.

protected void finalize()   // protected is used to prevent access by code defined outside its class.
{
     // finalization code here
}

Leave a comment