Write short notes on: a. Grid Layout b. Ragged Array

a. Grid Layout 

 The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle. Components are placed in columns and rows. Every Component in a GridLayout has the same width and height. Components are added to a GridLayout starting at the top-left cell of the grid and proceeding left to right until the row is full. Then the process continues left to right on the next row of the grid, and so on.



Example:

import java.awt.*;

import javax.swing.*;

public class GridDemo

{

JButton b1,b2, b3, b4, b5, b6; GridDemo()

{

JFrame f = new

JFrame("GridLayout Demo");

b1 = new JButton("A");

b2 = new JButton("B");

b3 = new JButton("C");

b4 = new JButton("D");

b5= = new JButton("E");

b6 = new JButton("F");

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.add(b5);

f.add(b6);

f.setLayout(new GridLayout(2, 3));

f.setSize(200, 200);

f.setVisible(true);

}

public static void main(String args[])

{

new GridDemo();

}

}

b. Ragged Array

Ragged Array

Ragged arrays are arrays of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These types of arrays are also known as Jagged arrays.

Syntax: int array_name = new int[row_size][column size];

// Program to demonstrate 2-D jagged array in Java 

class Main {

public static void main(String[] args)

{

// Declaring 2-D array with 2 rows

 int arr[][]= new int[2] []; 

// Making the above array Jagged

// First row has 3 columns 

arr [0] = new int[3];

// Second row has 2 columns

 arr [1] = new int[2];

// Initializing array

int count = 0;

for (int i=0; i<arr.length; i++)

for(int j=0; j<arr[i].length; j++)

 arr[i][j] count =++;

// Displaying the values of 2D Jagged array System.out.println("Contents of 2D Jagged Array"); 

for (int i=0; i<arr.length; i++)

{ for (int j=0; j<arr[i].length; j++) 

System.out.print(arr[i][j] + "); 

System.out.println();

}

}

}

Output:

Contents of 2D Jagged Array

0 1 2 

3 4


Comments

Popular posts from this blog

Electronic payments

What is the cloud cube model? Explain in context to the Jericho cloud cube model along with its various dimensions.

Computer Fundamental of TU IT Assistant