Friday, 3 January 2014

Array List Interview questions and answers

Q1. What is an ArrayList ? 

ArrayList is a class, part of Collection package in Java. It is  Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.  In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.(This class is roughly equivalent to Vector, except that it is unsynchronized.)  

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time.  The add operation runs in <i>amortized constant timethat is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking).  The constant factor is low compared to that for the LinkedList implementation.

Each ArrayList instance has a capacity. The capacity isthe size of the array used to store the elements in the list.  It is alway at least as large as the list size.  As elements are added to an ArrayList, its capacity grows automatically.  The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost. An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation.  This may reduce the amount of incremental reallocation.



Q1. What is the difference between ArrayList and Array ? 

ArrayList is growable in size whereas size of Array has to be defined at the time of initialization. When we are not sure about the how many no of elements required to be added in collection then we should use ArrayList. But if you are sure on size then use Array. 

Wednesday, 12 June 2013

Create XML File

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class CreateXMLFile {

       public static void main(String[] args) throws ParserConfigurationException, TransformerException {
             
              //get the Document Builder Factory instance
              DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
             
              //create the document Builder from Document Builder
              DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
             
             
              //create the new Document object from Document Builder
              Document doc = docBuilder.newDocument();
             
              //create root Element
              Element rootElement = doc.createElement("company");
             
              //append root element to the document
              doc.appendChild(rootElement);
             
              //create child Element
              Element employee = doc.createElement("Employee");
             
              //append child to root element
              rootElement.appendChild(employee);
             
              //create new Attribute
              Attr attr = doc.createAttribute("id");
              //set the value of the attribute
              attr.setValue("1");
             
              //attach it to element
              employee.setAttributeNode(attr);
             
              //first Element
              Element firstname = doc.createElement("firstname");
              firstname.appendChild(doc.createTextNode("Atul"));
              employee.appendChild(firstname);
             
             
              //last elements
              Element lastname = doc.createElement("lastname");
              lastname.appendChild(doc.createTextNode("vijay"));
              employee.appendChild(lastname);
             
              //create the DOMSource of the doc
              DOMSource source = new DOMSource(doc);
             
              //get the instance of the transformer factory
              TransformerFactory transformerFactory = TransformerFactory.newInstance();
             
              //create the new Transformer Object
              Transformer transformer = transformerFactory.newTransformer();
             
              //create the StreamResult object with File object
              StreamResult result = new StreamResult(new File("C:\\file.xml"));
             
              //create the file now
              transformer.transform(source, result);
       }
}