Multidimentional ArrayList (#5795)

* Multidimentional ArrayList

* Adding code for 3-D ArrayList

* Removing comment

* Fixing indentation

* Spellcheck
This commit is contained in:
rahusriv 2018-12-15 09:36:42 +05:30 committed by KevinGilmore
parent c7da8f3183
commit 6acf513fdb
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.list.multidimensional;
import java.util.ArrayList;
public class ArrayListOfArrayList {
public static void main(String args[]) {
int vertex = 5;
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(vertex);
//Initializing each element of ArrayList with ArrayList
for(int i=0; i< vertex; i++) {
graph.add(new ArrayList<Integer>());
}
//We can add any number of columns to each row
graph.get(0).add(1);
graph.get(0).add(5);
graph.get(1).add(0);
graph.get(1).add(2);
graph.get(2).add(1);
graph.get(2).add(3);
graph.get(3).add(2);
graph.get(3).add(4);
graph.get(4).add(3);
graph.get(4).add(5);
//Printing all the edges
for(int i=0; i<vertex; i++) {
for(int j=0; j<graph.get(i).size(); j++) {
System.out.println("Edge between vertex "+i+"and "+graph.get(i).get(j));
}
}
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.list.multidimensional;
import java.util.ArrayList;
public class ThreeDimensionalArrayList {
public static void main(String args[]) {
int x_axis_length = 2;
int y_axis_length = 2;
int z_axis_length = 2;
ArrayList< ArrayList< ArrayList<String> > > space = new ArrayList<>(x_axis_length);
//Initializing each element of ArrayList with ArrayList< ArrayList<String> >
for(int i=0; i< x_axis_length; i++) {
space.add(new ArrayList< ArrayList<String> >(y_axis_length));
for(int j =0; j< y_axis_length; j++) {
space.get(i).add(new ArrayList<String>(z_axis_length));
}
}
//Set Red color for points (0,0,0) and (0,0,1)
space.get(0).get(0).add("Red");
space.get(0).get(0).add("Red");
//Set Blue color for points (0,1,0) and (0,1,1)
space.get(0).get(1).add("Blue");
space.get(0).get(1).add("Blue");
//Set Green color for points (1,0,0) and (1,0,1)
space.get(1).get(0).add("Green");
space.get(1).get(0).add("Green");
//Set Yellow color for points (1,1,0) and (1,1,1)
space.get(1).get(1).add("Yellow");
space.get(1).get(1).add("Yellow");
//Printing colors for all the points
for(int i=0; i<x_axis_length; i++) {
for(int j=0; j<y_axis_length; j++) {
for(int k=0; k<z_axis_length; k++) {
System.out.println("Color of point ("+i+","+j+","+k+") is :"+space.get(i).get(j).get(k));
}
}
}
}
}