Last updated 1998 June 16 Roedy Green ©1996-1998 Canadian Mind Products.
Stuck in a frame? Click here to break out.// note how you never specify the array's size in its type declaration. int[][] mat; // for each row, allocate a slot for a pointer to an array mat = new int[3][]; for (int i=0; i<3; i++) { // allocate an array for each row mat[i] = new int[5]; for (int j=0; j<5; j++) mat[i][j] = i*j+100; }When you have a matrix with all columns the same size, you can allocate all the space at once this way.
int[][] mat = new int[3][5]; for (int i=0; i<3; i++) for (int j=0; j<5; j++) mat[i][j] = i*j+100;If you fail to initialise the array, Java automatically initialises it for you to to zeroes. If you have a matrix of objects, and you fail to initialise, Java initialises it to nulls for you. It does not allocate empty objects at each grid point for you. You have to allocate the objects yourself like this:
Cell[][] mat = new Cell[3][5]; for (int i=0; i<3; i++) for (int j=0; j<5; j++) mat[i][j] = new Cell(i,j,100);Here is how you could create a triangular matrix:
int[][] mat; // for each row, allocate a slot for a pointer to an array mat = new int[100][]; for (int i=0; i<100; i++) { // allocate an array for each row mat[i] = new int[i+1]; for (int j=0; j<=i; j++) mat[i][j] = i*j+100; }You can initialise a matrix to a list of values this way:
int[][] mat = {{11, 12, 13}, {21, 22, 21}, {31, 32, 33}};With JDK 1.1, you can assign a matrix to a list of values this way:
mat = new int[][] {{11, 12, 13}, {21, 22, 21}, {31, 32, 33}};In all these examples, you can use mat.length and mat[i].length to avoid repeating the constants that define the matrix's dimensions. See array, gotchas.
Extension | Mime Type | Notes |
---|---|---|
aif aiff aifc | audio/x-aiff | sound |
au snd | audio/basic | standard Internet/Java wave sound |
avi | video/x-msvideo | avi movie |
bas | text/plain | Basic source |
bin exe | application/octet-stream | executable program |
bmp | image/bmp | Windows image |
doc | application/msword | Microsoft Word document formatted. |
etx | text/x-setext | |
evy | application/envoy | Envoy |
gif | image/gif | standard Internet icon image |
gz | application/x-gzip | tar gzip |
html htm | text/html | HTML, web browser |
ief | image/ief | image |
jpeg jpg jpe | image/jpeg | stardard Internet photo image |
js | application/x-javascript | JavaScript |
mid | audio/midi | Crescendo MIDI sound |
mov qt | video/quicktime | Quicktime movie player |
movie | video/x-sgi-movie | movie |
mpg mpeg mpe | video/mpeg | mpeg movie player |
oda | application/oda | |
pbm | image/x-portable-bitmap | bitmap image |
application/pdf | Adobe Acrobat Portable Document Format | |
pgm | image/x-portable-graymap | grayscale image |
ppm | image/x-portable-pixmap | pixel image |
ps eps ai | application/postscript | Adobe PostScript, encapsulated PostScript |
ra rm ram | audio/x-pn-realaudio | Real Audio |
rgb | image/x-rgb | image |
rtf | application/rtf | rich text format |
rtx | text/richtext | rich text |
ssi shtml (htm html) | text/x-server-parsed-html | server side includes; web server expands embedded commands. Sometimes htm and html files are parsed for embedded commands too. |
tar | application/x-tar | Unix tar archive |
tiff tif | image/tiff | image |
tsv | text/tab-separated-values | tab separated list |
txt text | text/plain | text |
vew | application/groupwise | Novell GroupWise |
w61 | application/wordperfect6.1 | WordPerfect |
wav | audio/wav | Microsoft wave sound |
wav | audio/x-wav | Microsoft wave sound |
wp wpd wp5 | application/wordperfect | WordPerfect 5 |
w60 | application/wordperfect6.0 | Worderfect 6 |
zip | application/x-zip-compressed | WinZip |
In Java you take the remainder with the % operator. In Java, the sign of the remainder follows the dividend, not the divisor. Be especially careful when corralling random numbers into a smaller range. Java division does have the Euclidian property. When you multiply the quotient by the divisor and add the remainder you get back to the dividend. Java division is truncated division.
Floored division is what you normally want when trying to figure out which bin an item belongs in. You can compute floored division as:
if (dividend >= 0) ? (dividend / divisor) : ((dividend-divisor+1) / divisor);
For computing how many fixed-size bins you need to contain N items, you want ceiled division, also known as the covered quotient. You can compute the covered quotient as:
if (dividend >= 0) ? ((dividend+divisor-1) / divisor) : (dividend / divisor);
Signs | Division | Modulus |
---|---|---|
+ + | +7/+4=+1 | +7%+4=+3 |
- + | -7/+4=-1 | -7%+4=-3 |
+ - | +7/-4=-1 | +7%-4=+3 |
- - | -7/-4=+1 | -7%-4=-3 |
![]() |
![]() |
![]() |
|
Canadian Mind Products | The Mining Company's Focus on Java Best of the Net Award |
You can get an updated copy of this page from http://mindprod.com/jglossm.html |