forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multidimentional ArrayList (eugenp#5795)
* Multidimentional ArrayList * Adding code for 3-D ArrayList * Removing comment * Fixing indentation * Spellcheck
- Loading branch information
1 parent
c7da8f3
commit 6acf513
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...va-collections/src/main/java/com/baeldung/list/multidimensional/ArrayListOfArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...llections/src/main/java/com/baeldung/list/multidimensional/ThreeDimensionalArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |