java (arrays)

Question:
How do you create and use a multidimensional array of type int.

Answer:
C programmers are used to dealing with multidimensional arrays as chunksof contiguous memory which can be accessed through pointer arithmetic.In Java, an array is an actual object, and a multidimensional arraymust be constructed as a an array of arrays. It is an extremely convenientarrangement because you can access the length of an array from withinthe Java language, which allows you to make each array of a differentlength if necessary, without causing undue complications. But thedownside is that you must explicitly instantiate each array, which is notas efficient as allocating one big chunk of memory. To create a 3x3x3array of integers you would do the following:

public final class Matrix {  public static final void main(String[] args) {    int i, j;    int[][] matrix[];    matrix = new int[3][][];    for(i = 0; i < matrix.length; i++) {      matrix[i] = new int[3][];      for(j = 0; j < matrix[i].length; j++)	matrix[i][j] = new int[3];    }  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS