Playing with multi dimension array
package CollectionFramework;
import java.util.ArrayList;
import java.util.List;
public class MutiDimensionArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
MutiDimensionArray marry = new MutiDimensionArray();
Object[][] out = marry.fruitLibrary();
for (int i = 0; i < out.length; i++) {
for (int j = 0; j < out[i].length; j++) {
System.out.println(out[i][j]);
}
}
}
public Object[][] fruitLibrary() {
Object xxxxx[][] = { { 1, 2, 3, 4 }, { "A", "B", "C", "D" }, { "0.5" } };
Object xxxxxx[][] = { { "A", "B" }, { "A", "B", "C", "D" }, { "C", "D" } };
Object object[][] = { { "A", "B", "C", "D" } };
String string[][] = { { "A", "B", "C", "D" } };
return object;
}
}
//To check the size of 2D array
public static void MultiDimArray()
{
String fruits[][]= new String[3][4];
for (int i = 0; i < fruits.length; i++) {
for (int j = 0; j < fruits[i].length; j++) {
System.out.print(i);
System.out.println(j);
}
}
}
// for adding and retrieving elements to array
for (int i = 0; i < fruits.length; i++) {
for (int j = 0; j < fruits[i].length; j++) {
System.out.print(i);
System.out.println(j);
fruits[i][j]="Hello"; // Hello will be added in all index
System.out.println(fruits[i][j]);
}
}
// 2D array with both Rows and Columns
public static void MultiDimArray()
{
String fruits[][]= new String[3][2];
for (int i = 0; i < fruits.length; i++) {
for (int j = 0; j < fruits[j].length; j++) {
System.out.print(i);
System.out.println(j);
}
}
}
//Output
/*
00
01
10
11
20
21
*/
// 2D array with only rows and no column in each rows
public static void MultiDimArray()
{
String fruits[][]= new String[5][1];
for (int i = 0; i < fruits.length; i++) {
for (int j = 0; j < fruits[j].length; j++) {
System.out.print(i);
System.out.println(j);
}
}
}
//Output
/**
00
10
20
30
40
*/
For Data provider in TestNG
No comments:
Post a Comment