URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Sketchflow
  HTML https://sketchflow.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: Java libs code
       *****************************************************
       #Post#: 4--------------------------------------------------
       <Arrayable>
       By: Rasmrq Date: November 20, 2019, 4:22 pm
       ---------------------------------------------------------
       GSON library is required, if for some reason you cannot import
       this library, delete the toJSON () method The library will no
       longer be needed, but then you will lose the ability to convert
       the list into a JSON string  This is a list that can store both
       values and other lists.  The list can store any type of values,
       but only the one that was set in the original list (in fact, it
       is easy to manage, but it's better not to risk it) The code was
       tested in Processing 3 (A program using Java-based language, so
       that for those who know Java, this program will be easy) and
       everything works perfectly ...
       class ArrayableArray extends ArrayList<Object> {
       public Class type;
       public ArrayableArray(Class type) {
       this.type = type;
       }
       public ArrayableArray() {
       this.type = Integer.class;
       }
       @Override
       public boolean add(Object obj) {
       if(obj.getClass() == this.type) {
       super.add(obj);
       } else {
       if(obj instanceof ArrayableArray) {
       ArrayableArray arr = (ArrayableArray)obj;
       if(arr.type == this.type) {
       super.add(obj);
       } else {
       throw new IllegalArgumentException("Given array type does not
       match the parent Array type. Given
       type:"".concat(arr.type.toString()).concat("" Array
       type:"").concat(this.type.toString()).concat("""));
       }
       } else {
       throw new IllegalArgumentException("Given value type does not
       match the Array value type. Given
       type:"".concat(obj.getClass().toString()).concat("" Array
       type:"").concat(this.type.toString()).concat("""));
       }
       }
       return true;
       }
       @Override
       public void add(int index,Object obj){
       if(obj.getClass() == this.type) {
       super.add(obj);
       } else {
       if(obj instanceof ArrayableArray) {
       ArrayableArray arr = (ArrayableArray)obj;
       if(arr.type == this.type) {
       super.add(obj);
       } else {
       throw new IllegalArgumentException("Given array type does not
       match the parent Array type. Given
       type:"".concat(arr.type.toString()).concat("" Array
       type:"").concat(this.type.toString()).concat("""));
       }
       } else {
       throw new IllegalArgumentException("Given value type does not
       match the Array value type. Given
       type:"".concat(obj.getClass().toString()).concat("" Array
       type:"").concat(this.type.toString()).concat("""));
       }
       }
       }
       public String toJSON() {
       String str = null;
       try{
       Gson gson = new Gson();
       JsonElement jsonArray = gson.toJsonTree(this, new
       TypeToken<List<Object() {}.getType());
       str = jsonArray.toString();
       } catch(NoClassDefFoundError e) {
       println("GSON not defined.cannot convert array to json");
       }
       return str;
       }
       public ArrayableArray getArray(int index) {
       ArrayableArray ret;
       if(super.get(index) instanceof ArrayableArray) {
       ret = (ArrayableArray)super.get(index);
       } else {
       throw new
       IllegalArgumentException("Excepted:"".concat(this.getClass().toS
       tring()).concat("",but
       got:"").concat(super.get(index).getClass().toString()).concat(""
       "));
       }
       return ret;
       }
       }
       void setup() {
       ArrayableArray arr = new ArrayableArray(Integer.class);
       arr.add(1);
       arr.add(new ArrayableArray(Integer.class));
       arr.getArray(1).add(1);
       arr.getArray(1).add(8);
       arr.getArray(1).add(new ArrayableArray(Integer.class));
       arr.getArray(1).getArray(2).add(7);
       arr.getArray(1).getArray(2).add(1);
       arr.getArray(1).add(5);
       arr.add(6);
       println(arr.toJSON());
       }
       #Post#: 5--------------------------------------------------
       Re: &lt;Arrayable&gt;
       By: pace Date: November 20, 2019, 4:32 pm
       ---------------------------------------------------------
       :D, wow thanks
       *****************************************************