Results 1 to 20 of 35
- 06-21-2011, 02:31 PM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
understanding the method "length" and how to use it
Hello again,
so in the following code, again from the Java oracle tutorial, there is a this little line below (in red) which is confusing me. I don't understand why it is length and not length()? Also, why is nothing imported? no package, nothing, so what package does it come from?
Thank you!Java Code:public class Adder { public static void main(String[] args) { [COLOR="red"]int numArgs = args.length; [/COLOR] //this program requires at least two arguments on the command line if (numArgs < 2) { System.out.println("This program requires two command-line arguments."); } else { int sum = 0; for (int i = 0; i < numArgs; i++) { sum += Integer.valueOf(args[i]).intValue(); } //print the sum System.out.println(sum); } } }
- 06-21-2011, 02:53 PM #2
Because it's a field, not a method.I don't understand why it is length and not length()
Names
Arrays (The Java Tutorials > Learning the Java Language > Language Basics)
db
- 06-21-2011, 02:57 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
length is a public attribute of an array.
There is no length() method.
As for no imports, that code is not using anything that isn't in the java.lang package, so no import statements are needed.
- 06-21-2011, 04:01 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
How do you know that? Where in the language did you find that? I'm just looking for a class or package where I can read about it. I found the example DB, but I still don't know where it comes from. Also, speaking of arrays:
The array class defined in the API:
java.lang.Object
extended by java.lang.reflect.Array
has a This method:
static int getLength(Object array)
Returns the length of the specified array object, as an int.
Why couldn't I use it o get the size of an array?
- 06-21-2011, 04:10 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Because an array (ie int[]) is not an Array.
It is a built in thing, which has a built in property called length.
From the tutorial in Darryl's post:
Now, you could use that static getLength method in Array if you wanted, but it would be a bit pointless.Finally, you can use the built-in length property to determine the size of any array.
- 06-21-2011, 04:12 PM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Oh okay. Thanks!
- 06-21-2011, 04:12 PM #7
You could use the method call but its the long way around:
int[] anArray = new int[5];
System.out.println("length=" + anArray.length + " and " + java.lang.reflect.Array.getLength(anArray));
>>> length=5 and 5
- 06-21-2011, 04:19 PM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 06-21-2011, 04:20 PM #9
Try it and see what happens. Then you'll know and can tell us what happens.
- 06-21-2011, 05:27 PM #10
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Sweet. Alright, I tried both:
So obviously, your way with the full named worked out.
Without it I get an error "cannot find symbol"
So I added an import statement:
and usedJava Code:import java.lang.reflect.*;
and it worked. Now I have a new question. Tolls made the point of indicating that array and Array are not the same which I understand that Java is case-sensitive, so why did it work?Java Code:Array.getLength(myarray);
- 06-21-2011, 05:29 PM #11
What is the "it" that worked?so why did it work?
- 06-21-2011, 05:34 PM #12
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Oh sorry.
The size of the array was returned using either statements that is array.length; and java.lang.reflect.Array.getLength(array);
so then when I imported the class, the shorter statement Array.getLength(array); also returned the size of the array.
- 06-21-2011, 05:39 PM #13
That's what import statements allow you to do when referring to class names.
When you import the package, you can code the class name without the package name.
There can be confusion when you use variable names the same as class names but with different case: array and Array.
Better to use your own unique variable name: myArray
- 06-21-2011, 05:39 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Which is what that method does.
Why is that odd?
- 06-21-2011, 05:49 PM #15
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Okay, thanks.
hello Tolls, this is (or was) odd to me because I was mistakenly assuming that the word array was a reserved word in Java and that it meant something. It was my fault, all part of learning.
I now have another question though:
http://download.oracle.com/javase/6/...ect/Array.html
If you go there, there is these couple of lines which are now throwing me off:
I attempted to create an array using the code in red and I entered the number 16. When I tried to ask for the length, it returned the number 1. Am I missing something?
Originally Posted by Java API Array Class
Why is that?Java Code:int[] x = {16}; system.out.println(x.length); //prints 1 int[] x; //declares array of integers named imgNum x = new int[16]; //allocates memory for 16 integers System.out.println(x.length); //prints 16
Is there a single statement way of declaring an array AND to allocate the right memory size for it? If so, what is it please :)
Thanks!
- 06-21-2011, 05:56 PM #16
The newInstance method takes an array for a parameter. Read the doc for what the contents of that array is for. It looks like the contents here is the length of something. Perhaps the length of the first dimension of the array being created. Just a guess. I have not read the doc.
int[] anArray = new int[25]; // create an array with 25 elementsIs there a single statement way of declaring an array AND to allocate the right memory size for it?
- 06-21-2011, 05:57 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Sure, an initializer can do it:
Does this clarify things a bit?Java Code:int[] a= { 1, 2, 3 }; // creates an array with three elements, 1, 2 and 3 int[]b= new int[3]; // also creates an array with three elements, all zero int[]c= { 3 }; // creates an array with one element, its value is 3
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-21-2011, 05:58 PM #18
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 06-21-2011, 06:04 PM #19
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
I went back and re-read it and I don't get it. It uses {} and states this about the parameters
So wouldn't that be [10] instead of {10}? Also, the Class object representing component type, I'm assuming that this means Integer, Character, String, etc...Is that a correct assumption?Parameters:
componentType - the Class object representing the component type of the new array
length - the length of the new array
- 06-21-2011, 06:11 PM #20
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
differentiate between user selection and "additem" method in combobox
By rainman3 in forum New To JavaReplies: 2Last Post: 04-28-2009, 04:52 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks