11 Replies - 573 Views - Last Post: 11 August 2009 - 04:33 PM Rate Topic: -----

#1 luron31  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 19-July 09

Can anyone explain this string output for me?

Post icon  Posted 10 August 2009 - 08:36 AM

Can anyone give me a line by line explanation as to why this is correct?

String word = “comp”;
for(int j = 0; j < 4; j++){
IO.printString(word.substring(0, j));
}
 


final output is:

“”
“c”
“co”
“com"

Is This A Good Question/Topic? 0
  • +

Replies To: Can anyone explain this string output for me?

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: Can anyone explain this string output for me?

Posted 10 August 2009 - 08:38 AM

String is an array. Look up properties of an array.
Was This Post Helpful? 0
  • +
  • -

#3 luron31  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 19-July 09

Re: Can anyone explain this string output for me?

Posted 10 August 2009 - 08:56 AM

oh yeah im sorry thats what i meant
Was This Post Helpful? 0
  • +
  • -

#4 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Re: Can anyone explain this string output for me?

Posted 10 August 2009 - 09:00 AM

your string variable 'word' is being read into a for loop, as per the condition set out in the for loop. And also, a substring of 'word' means the loop is breaking up the word for you.

You see, take the criteria word.substring(0,j) where j is set to 0, that will obviously print nothing, and as you follow the for loop, with the next word.substring(0,j) where j is set to 1 now, because in your for loop you will notice your last condition in the for loop j++ where j got incremented by 1. This will now print out your first character in word which is 'c'.

Do I need to continue or you get the picture? Do a trace table like I roughly did replacing j with number 0 right upto 3, not 4 because your second condition states j < 4.
Was This Post Helpful? 1
  • +
  • -

#5 luron31  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 19-July 09

Re: Can anyone explain this string output for me?

Posted 10 August 2009 - 09:04 AM

THANK YOU! that makes sense to me now
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: Can anyone explain this string output for me?

Posted 10 August 2009 - 09:27 AM

substring(int, int) shows where the pointers read from.

j = 0
**comp

j = 1
*c*omp

j = 2
*co*mp

j = 3
*com*p

It just's puts the pointer in front of the index given. Remember that arrays start at 0. ^__^
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Can anyone explain this string output for me?

Posted 10 August 2009 - 03:49 PM

View PostNeoTifa, on 10 Aug, 2009 - 07:38 AM, said:

String is an array. Look up properties of an array.

What !!!! :crazy:

String is an object
may be containing an array of char[] but it will be against any OO philosophy to assume that
may be String keep, or will eventually, hold its elements in an array, an ArrayList, a Vector, a LinkedList, a Stack, you don't know and you don't want to know
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: Can anyone explain this string output for me?

Posted 11 August 2009 - 07:17 AM

I was told that it was essentially an array, and so I think of them that way.
Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Can anyone explain this string output for me?

Posted 11 August 2009 - 07:28 AM

You were really taught to think of them as an Array? Thats a new one to me. As PBL says, you could create a String from a char array, or vise versa, wouldnt suggest it though. Not trying to catch you out here :P but if you create an Array of type String, why would you have to generically label it as a String?
Was This Post Helpful? 0
  • +
  • -

#10 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: Can anyone explain this string output for me?

Posted 11 August 2009 - 07:31 AM

I think of them as arrays as you can index and manipulate them as such.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Can anyone explain this string output for me?

Posted 11 August 2009 - 04:31 PM

View PostNeoTifa, on 11 Aug, 2009 - 06:31 AM, said:

I think of them as arrays as you can index and manipulate them as such.

That is correct... it behaves like an array so your last quote is correct.... and hope it will continue to do so, if not a lot of programs will be in trouble

but that one

Quote

I was told that it was essentially an array,

can really introduced a wrong concept

even if you kind of correct it with

" and so I think of them that way."

:^:

This post has been edited by pbl: 11 August 2009 - 04:32 PM

Was This Post Helpful? 0
  • +
  • -

#12 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: Can anyone explain this string output for me?

Posted 11 August 2009 - 04:33 PM

I was told how to think.... :ph34r:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1