开发者

Printing an array

I have an array of 28 characters. I'm trying to use a for loop to print the string and i开发者_StackOverflow社区ts length so that the next line removes the last character from the previous line. For example:

7 Example
6 Exampl
5 Examp
4 Exam
3 Exa
2 Ex
1 E

I have this so far, but it's not printing. What am I doing wrong?

for(int index=28; index <= nameArray.length; index--)
            {
                System.out.print(nameArray[index]);
            }


It is as simple as that ..

class A {
    public static void main(String [] args) {
        String array = "Example";

    for(int i=7;i>0;i--) {  
          System.out.print(i+" ");    
          System.out.println(array.substring(0, i)); 
        }
      } 
   }  

// Output :   
7 Example
6 Exampl
5 Examp
4 Exam
3 Exa
2 Ex
1 E


If the length is 28 characters, you cannot start the index at 28. Arrays in Java are zero-based, so that means that the index can range from 0 to 27 (length - 1).


Firstly, you probably want your middle condition to be index > 0, not index <= nameArray.length (which should always be true, even as you enter the negative numbers).

Secondly, is it an array of characters you're accessing? If so, then as I'm sure you know, nameArray[index] refers to a character, not a string.

(Also, as bobbymcr points out, the top index in an array of 28 elements is 27, not 28.)

To print substrings, you should first initialize a String using your character array, then print increasingly smaller substrings using substring.

For example:

private static void printSubstrings(String input) {
    for (int length = input.length(); length > 0; length--) {
        System.out.printf("%d %s\n", length, input.substring(0, length));
    }
}

Then, if nameArray is a char[], you could use the String(char[]) constructor to pass to this method:

printSubstrings(new String(nameArray));

Example input:

char[] nameArray = new char[] { 'E', 'x', 'a', 'm', 'p', 'l', 'e' };
printSubstrings(new String(nameArray));

Output:

7 Example
6 Exampl
5 Examp
4 Exam
3 Exa
2 Ex
1 E


Always remeber that the array indexes start from 0

[E][x][a][m][p][l][e] 
 0  1  2  3  4  5  6

I don't understand why you break your head with char arrays, when you have String. Here i propose you a solution where:

1-transform the char[] to String

2-Iterate the String object and use the some methods from the String class to get the desired result:

public class Example
{
   public static void main(String[] args) {

       //Here an array with 28 characters
       char[] array = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'
           ,'q','r','s','t','u','v','w','x','y','z','!','@'};

      //For easier manipulation i will just transform that array to String
      //Why not, String was invented so we dont have to use char[].
      String arrayAsString = new String(array);

    //Use the methods that String class provides, to make the solution easier    
    for(int i=0;i < array.length;i++) {    
      System.out.println((i+1) + " " +  arrayAsString.substring(0,(arrayAsString.length()-i)));
    }
    }
}


for(int index=28; index <= nameArray.length; index--)

First off, you're trying to start at index 28 - that doesn't exist. Arrays are 0 based which means they go from 0 to length - 1

Second, you're saying that the index has to be less than or equal to the length. Since you're going from the end to the start, that would always be true.

for (int index = nameArray.length - 1; index >= 0; index--)

That's what you're looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜