0

I have a program and I need to understand it, but I don't understand two lines of it. Okay so there is one-dimensional array - int [] names, and two chars - char let1, let2. Now, there is a command:

char let1 = names[i].charAt(names[i].length()-1);
char let2 = names[i+1].charAt(0);

What does that mean?

3
  • This code wouldn't compile if names is actually an int[]. It looks pretty clear that it's a String[]. Now which specific bit of that code don't you understand? Commented Mar 30, 2013 at 9:41
  • Are you sure that 'names' type is int? Commented Mar 30, 2013 at 9:42
  • length is an attribute if it is int[], charAt is a method and length is also a method for String. Commented Mar 30, 2013 at 9:43

2 Answers 2

1

let1 is assigned the last character of names[i] and let2 is assigned the first character of names[i+1].

Sign up to request clarification or add additional context in comments.

Comments

0
char let1 = names[i].charAt(names[i].length()-1);

It means find out the string at index i of String array names and from that String extract out the character at the last index of that String. And then assign that character value to the char variable let1.

char let2 = names[i+1].charAt(0);

It means extract out the String at index i+1 from String array names and from that String extract out the character at first index (0) . And then assign that character value to the char variable let2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.