This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes �Understanding this program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "�Understanding this program?" Watch "�Understanding this program?" New topic
Author

�Understanding this program?

Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I am just, and I mean "just"..lol learning Arrays and Strings.. I am having trouble grasping many concepts such as the use of String Arrays in the following code in my book..


I tried to understand how all of this works and figured out some, but not much.. I don't get indexOf(), or substring(), or the length() ..and when you do the Array brackets [] that just throws my Exception..lol No but it really distracts me and overwhelms me.. I feel I am expected to know all of this well, but can't seem to grasp it well yet .. I still have trouble understanding what an Object is ..

Sorry for the lengthy post, but if someone could please help me understand these things better I would be forever greatful..
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

When there's a method that you don't understand, you should check the API first. It really helps : http://java.sun.com/j2se/1.4.2/docs/api/
Look for the Array class and check its methods.


[My Blog]
All roads lead to JavaRanch
Surajit Sahu
Greenhorn

Joined: Sep 06, 2006
Posts: 2
hi,
String class have lots of methods which can be used for different types of operations such as length():for finding out the length of the string,indexat():it will give the chracter at a particular index..........

so getting a better idea of different methods,you can refer the java API classes.

Regarding Object:Its the blue print of class.So once you will creat a object of a class,it can inherite all the properties of the class.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Satou kurinosuke:
When there's a method that you don't understand, you should check the API first. It really helps : http://java.sun.com/j2se/1.4.2/docs/api/
Look for the Array class and check its methods.


Although that's a valid link to the API documentation -- although it's to an old version, not likely to be the one the OP is using -- the suggestion that he check the Array class is... well, rather misguided. You surely meant to recommend the java.lang.String class documentation here.

The Array class is for advanced uses only, as it has to do with the innards of the JVM itself. It has nothing to do with the program above.


[Jess in Action][AskingGoodQuestions]
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
So I could probably get by fine without using arrays in a program?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
We're crossing up two things here. Using arrays is fine and not too hard once you get a good mental picture of what they do. The Array class is a different story. Don't worry about it at all for a long while.

As far as that mental picture ... imagine a row of boxes on the floor. You can put something in the first box, the last box or any in between. Call the first one box[0] and the last one box[boxCount-1] and you're doing arrays.

With an array of primitives every box will have something in it, initially the default value for the primitive (eg 0 for int) and later maybe something else you put in it.

With an array of objects the boxes start out empty (or null) until you put something in them.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You surely meant to recommend the java.lang.String

Ernest, you're right, I mixed up Array and String. Sorry.

I don't consider 1.4.2 to be an old version and still refer to it everyday
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
well, for the methods you dont understand,

length() is just to return the length of a string.

example:

String a = "hello";
System.out.print(a.length());

it will print out 5, because "hello" is 5 characters long.

for indexOf(),

again example:

String a = "hello";
System.out.print(a.indexOf(0));

it will print out h, because the index of 'h' in the string "hello"
is 0,

H E L L O
0 1 2 3 4

so when it says, if somestring.indexOf(theCharInputted) > -1,
it's just seeing if the character is in the string..

get it?

and i saw some examples of simple arrays above...

[] just denotes an array

int [] A = new int [4];

just declares an array object of type int, of length 4.
and you can only store ints in the array after it is declared.

another way to set up an array is like so:

int [] A = {1,2,3,4};

this will create an int array of size 4, but actually assign int
values to each index of the array.

because, it the first example, all the index references are null.

and for the method substring()

again, say you have:

String A = "Hello";
System.out.println(A.substring(1,3));

it would print "el".

well atleast im pretty positive it will lol

because if i remember correctly, it includes the character at index
1, but does not include the character at index 3.

so here is the visual again:

H e l l o
0 1 2 3 4

so if we call A.substring(1,3);

1 is an index of the string A, and 3 is too...

so if we include the index 1 we get "e", then we go to 2, because we haven't

hit 3 yet, the the string becomes, "el", then we go to 3, ah there's three,

so we stop and just have the substring "el"

well i hope this helped a little bit,

Justin


You down with OOP? Yeah you know me!
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
Thanks, I really appreciate you breaking it down like that.. The book just threw it at me and didn't explain it that well..
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
no problem, any more questions, just ask! : )

Justin
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

for indexOf(),

again example:

String a = "hello";
System.out.print(a.indexOf(0));

it will print out h, because the index of 'h' in the string "hello"
is 0,

Slight correction.


String a = "hello";
System.out.print(a.indexOf(0));

Will print:
-1


String a = "hello";
System.out.print(a.charAt(0));

Will print:
h


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
 
subject: �Understanding this program?