Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Array definition confusion

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, I have compiled the following code declaring 2 arrays:



and I get the following compilation error:

Cert.java:7: incompatible types
found : int[]
required: int[][]
pant = new int [5];



If I declare them on separate lines, like so



it works! What's going on here? Why does the compiler think my "pant" array is 2 dimensional?
 
author & internet detective
Posts: 40801
829
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start with simpler examples and work up to yours.

Two ints

This code clearly creates two ints. It is like writing int ant; int pant;

Two arrays

This code creates two int[] references. It is like writing int[] ant; int[] pant; So far so good? The type is "distributed" across all variables declared in that line.

Your example

This code declares two arrays. "Distributing" the initial int[] type, we have int[] ant and int[] pant[]. The first is the one dimensional array. The second is a two dimensional array. It's just that the parts were defined in a "less than clear" fashion. Something that does happen on the exam.
 
Anda Popovici
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the explanation, makes sense now!

The fact that the [] were like "int []a" was even more confusing.

 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:"Distributing" the initial int[] type, we have int[] ant and int[] pant[]. The first is the one dimensional array. The second is a two dimensional array.


Ah, sorry Jeanne but I have to disagree ;).
The type int[][] is a one-dimensional array that stores int[] references. Java does not have two-dimensional arrays.
Nah... It's just me ;). I tend to nitpick too often :P.
 
Jeanne Boyarsky
author & internet detective
Posts: 40801
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:The type int[][] is a one-dimensional array that stores int[] references. Java does not have two-dimensional arrays.
Nah... It's just me ;). I tend to nitpick too often :P.


Pawel is correct from a technical perspective. From the point of view of the original poster asking "Why does the compiler think my "pant" array is 2 dimensional?" I feel using "two-dimensional" array is appropriate. In fact, I find it appropriate in general . 2D array is a nice shorthand for an array of arrays. Or an array of array references...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic