Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

array_problem

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Wondering about the following code.
int s[];
s=new int[3];
s[0]=new int[2];
Why does this cause compile error?
j
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you declare the array size and dimension it cannot be altered any more. In this instance you are trying to put a new array into the first element of the original array. The result would alter the array into two dimensional array.
If you want to have multi dimensional arrays you have to declare it like
int s1[][];
int s2[][][];
etc.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mika Leino:
Once you declare the array size and dimension it cannot be altered any more. In this instance you are trying to put a new array into the first element of the original array. The result would alter the array into two dimensional array.
If you want to have multi dimensional arrays you have to declare it like
int s1[][];
int s2[][][];
etc.


I agree that you cannot change the dimension of an array once declared. There are questions and answers in Kathy Sierra(?)'s book that indicate that an array's size (length) can be altered, like so:


But changing the length by setting the length variable does not work.
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Derek -
If you tell me where in the book you found that, I'll take a look and let you know what I think!
-Bert
 
Derek Baker
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can be more specific about it tonight, but I believe it was one of the final self test questions after Chapter 1. I should point out it is true that you can change array size. The following code compiles and runs just fine:
 
Bert Bates
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Derek -
In your example, what would you call ints1 and ints2? Are they arrays? Or are they references to arrays? What are you really copying here? Are you really changing the size of an array?

-Bert
 
Derek Baker
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind, he said sheepishly.
I have trouble remembering that that distinction exists. Is it appropriate to think of arrays as immutable in the same way that Strings are?
So, what it comes down (I think) to is that you can point an array reference to an array of a different length but of the same dimension. But you cannot point a one-dimensional array reference to a two-dimensional array.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by janne kallio:
Hi!
Wondering about the following code.
int s[];
s=new int[3];
s[0]=new int[2];
Why does this cause compile error?
j


To make things clear here... int s[] declares an array with elements of type int.
So s[0] is of type "int" now in the step:
s[0] = new int[2]
you creat an object - Array - using the new operator and assign you try to assign the Array object to a variable of type int... clearly illegal operation and that's why you get a compile error.
Derek, Arrays are immutable objects, yet their elements are normal variables of any type that can have their values changed. As you can not change the length of a string, you can not change the length of an array.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic