Core Java Tutorial

Define Java

C vs C++ vs Java

Java Virtual Machine

Java First Program

Java Data Types

Java Tokens

Java Keywords

Java Operators

Operator Precedence

Java Type Casting

Java Statement

Java If statement

Java Switch statement

Java Loops

Java Jumping Statement

Java Arrays 1D

Java Arrays 2D

Java Variable Size Array

Java Vector

Java Math Methods

Java String Methods

Java String Buffer

User-Defined Method

Java Method Overloading

Java Class & Object

Java Constructor

Java Inheritance

Java Interface

Java Packages

Java Multi-threading

Exceptional Handling

File Handling

Java Applets

Java DataBase Connectivity

Page Stats

Visitor: 319

Java - Variable Size Array

If the column size in the arrays are in zig-zag form means not fixed for every row than variable size array is useful.

Syntax to declare variable size array

int x[][] = new int [3][];
x[0] = new int [2];
x[1] = new int [4];
x[2] = new int [3];

Example 1: Input 3 student marks using variable size array.

package variablesizearray;

import java.util.Scanner;
public class Variablesizearray {
    public static void main(String[] args) {
        int i, j;
        int a[][] = new int [3][];
        a[0] = new int[3];
        a[1] = new int[5];
        a[2] = new int[4];
        
        Scanner scan = new Scanner(System.in);
        for(i=0;i<=2;i++) //row
        {
            System.out.println("Enter " + (i+1) + " student marks");
            for(j=0;j<a[i].length;j++) //col
            {
                a[i][j] = scan.nextInt();
            }
        }

        for(i=0;i<=2;i++)
        {
            System.out.println((i+1) + " student marks is: ");
            for(j=0;j<a[i].length;j++) //col
            {
                System.out.print(a[i][j]);
            }
            System.out.println("\n");
        }
    }
}
Enter 1 student marks 1 2 3
Enter 2 student marks 1 2 3 4 5
Enter 3 student marks 1 2 3 4
1 student marks is: 1 2 3
2 student marks is: 1 2 3 4 5
3 student marks is: 1 2 3 4
Updated: 28-Feb-20