In this post we will provide a few examples on how to initialise an array in Java.

#### Single dimensional array:

Below are some examples on how to initialise a single dimensional array including initialise an array in one line.

1 |
int[] numbers = new int[3]; |

1 |
int[] numbers = {1,2,3}; |

1 |
int[] numbers = new int[]{1,2,3}; |

1 |
Integer[] numbers = {1,2,3}; |

1 |
Integer[] numbers = Arrays.array(1, 2, 3); |

#### Multi dimensional array:

Below are some examples on how to initialise a multi dimensional array

1 2 3 4 5 |
int[][] multiarray = new int[2][2]; multiarray[0][0] = 1; multiarray[0][1] = 2; multiarray[1][0] = 3; multiarray[1][1] = 4; |

1 |
int[][] multiarray = {{1,2}, {3,4}}; |

I hope the above examples has helped you understand how to create an array.

#### Similar posts: