Thursday, February 14, 2019

Java Collections!

Java Collections and Iterators
Java collection framework includes:
- Interface
- Class of implementation
- Algorithm

Methods
public boolean hasNext()
public object next()
public void remove()
Java Multi Dimentional Arrays

Examples
import java.io.*;
import java.util.*;
import static java.lang.System.*;
// import java.util.ArrayList;
// import java.util.Arrays;
// import java.util.List;


public class ary2d{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("my.in"));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("my.out")));

        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());

        ArrayList<String> list = new MyArrayList<String>("a", "b", "c");
        ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();

        ArrayList<ArrayList<String>> biDemArrList = new ArrayList<ArrayList<String>>();
        ArrayList<String> temp = new ArrayList<String>(); // added () 
        temp.add("Hello world.");
        biDemArrList.add(temp);

        ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();

        // List<ArrayList<Integer>> a = new ArrayList<>(); // java 1.7+
        List<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();

        ArrayList<Integer> a1 = new ArrayList<Integer>();
        a1.add(1);

    }
}

class Array2D<T> extends ArrayList<ArrayList<T>> {
    public void addToInnerArray(int index, T element) {
        while (index >= this.size()) {
            this.add(new ArrayList<T>());
        }
        this.get(index).add(element);
    }

    public void addToInnerArray(int index, int index2, T element) {
        while (index >= this.size()) {
            this.add(new ArrayList<T>());
        }

        ArrayList<T> inner = this.get(index);
        while (index2 >= inner.size()) {
            inner.add(null);
        }

        inner.set(index2, element);
    }
}

class TwoDimArray<T> extends ArrayList<ArrayList<T>> {
    public void addToInnerArray(int index, T element) {
        while (index >= this.size()) {
            // Create enough Arrays to get to position = index
            this.add(new ArrayList<T>()); // (as if going along Vertical axis)
        }
        // this.get(index) returns the Arraylist instance at the "index" position
        this.get(index).add(element); // (as if going along Horizontal axis)
    }

    public void addToInnerArray(int index, int index2, T element) {
        while (index >= this.size()) {
            this.add(new ArrayList<T>());// (as if going along Vertical
        }
        //access the inner ArrayList at the "index" position.
        ArrayList<T> inner = this.get(index);
        while (index2 >= inner.size()) {
            //add enough positions containing "null" to get to the position index 2 ..
            //.. within the inner array. (if the requested position is too far)
            inner.add(null); // (as if going along Horizontal axis)
        }
        //Overwrite "null" or "old_element" with the new "element" at the "index 2" ..
        //.. position of the chosen(index) inner ArrayList
        inner.set(index2, element); // (as if going along Horizontal axis)
    }
}
Java Collections
    ArrayList astr=new ArrayList();
    astr.add("john");
    Iterator itr=astr.iterator();
    while(itr.hasNext()){
        System.out.println(itr.next());
    }

No comments:

Post a Comment