简体   繁体   中英

How to implement Rootish Array Stack Data Structure in Java, using jagged array or pointer array

Basically i just want to know that how can i get an array which contains another array and i could shrink it or grow it, For example A{a,b,c,d} a{0}, b{1,2} c{3,4,5} d{6,7,8,9} . And if i want to add another integer in it then i can grow it A{a,b,c,d,e} a{0}, b{1,2} c{3,4,5} d{6,7,8,9} e{10} in java.

Rootish Array Stack is basically a data structure just like array which stores array in the way i mentioned above.....

I tried using jagged array but i cannot grow it or shrink it

This is simply a List of List . You can take help from below example code. The size of the List can increase or decrease as you add/remove elements.

List<List<Integer>> listOfLists = new ArrayList<List<Integer>>();

import java.util.ArrayList;
import java.util.List;

public class ListOfLists {

  public static void main(String args[]) {
    List<List<Integer>> listOfLists = new ArrayList<List<Integer>>();

    List<Integer> aList = new ArrayList<Integer>();
    List<Integer> bList = new ArrayList<Integer>();
    List<Integer> cList = new ArrayList<Integer>();
    List<Integer> dList = new ArrayList<Integer>();

    aList.add(0);
    bList.add(1); bList.add(2);
    cList.add(3); cList.add(4); cList.add(5);
    dList.add(6); dList.add(7); dList.add(8); dList.add(9);

    listOfLists.add(aList);
    listOfLists.add(bList);
    listOfLists.add(cList);
    listOfLists.add(dList);

    for (List<Integer> list : listOfLists) {
      for (Integer i : list) {
        System.out.print(i + "\t");
      }
      System.out.println();
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM