• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

�Understanding this program?

 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Surajit Sahu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I could probably get by fine without using arrays in a program?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no problem, any more questions, just ask! : )

Justin
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic