Jagged arrays are also known as ragged arrays. They are the arrays containing arrays of different length.
Consider the below example in which each row consists of different number of elements. First row contains 4 elements, second row contains 2 elements and third row contains 3 elements.
Example
package com.w3schools; import java.util.Arrays; public class Test { public static void main(String args[]){ int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[]{0,1,2,3}; jaggedArray[1] = new int[]{4,5}; jaggedArray[2] = new int[]{6,7,8}; for(int[] row : jaggedArray){ System.out.println(Arrays.toString(row)); } } } |
Output
[0, 1, 2, 3] [4, 5] [6, 7, 8] |
Interview Questions on Arrays
- Can we change array size in java?
- What is an anonymous array in java?
- Difference between array and arraylist in java?
- What are jagged arrays in java?
- Can array size be negative in java?
- Java program to find duplicate elements in an array.
- Java program to find second largest element in an array of integers.
- Java program to check the equality of two arrays.
- Find all pairs of elements in an integer array whose sum is equal to a given number.
- Java program to find continuous sub array whose sum is equal to a given number
- Java program to find the intersection of two arrays
- Java program to separate zeros from non-zeros in an integer array
- Java program to find all the leaders in an integer array
- Java program to find a missing number in an integer array
- Java program to convert an array to ArrayList and an ArrayList to array
- Java program to count occurrences of each element in an array
- Java program to reverse an array without using an additional array
- Java program to remove duplicate elements from an array
- Java program to find union and intersection of multiple arrays
- Java program to find the most frequent element in an array