簡體   English   中英

向右移動數組的所有元素

[英]Move all elements of an array to the right

因此,我得到了一個大小為22的數組,該數組從控制台讀取一個數字並將該數字添加到該數組中。 因此,如果在數組中讀取了123456,則會打印出

12345600000000 ...沿着這些思路。

我需要正確地證明這個數字以執行算術運算,但是我似乎無法獲得正確的循環結構來輸出數字!

int[] newlong = new int[22];
String inputline = "";

public void readNewLong()
{
    Scanner in = new Scanner(System.in);
    inputline = in.nextLine();

    try
    {
         for (int i = 0; i < inputline.length(); i ++)
         {
              char a = inputline.charAt(i);
              String b = "" + a;
              newlong[i] = Integer.parseInt(b);
         }

    }
    catch (NumberFormatException nfe)
    {
        System.out.println("NumberFormatException, you must enter a string of digits. " + nfe.getMessage());
    }
}

這就是讀取數字,輸入行並將其存儲在數組newLong中的內容。

下面的方法是行不通的...

public void rightJustify()
{
     printLong();
     System.out.println(inputline.length());

     for(int i = 0; i< inputline.length(); i++)
     {
         for(int j = 0; j < newlong.length; j++)
         {
              newlong[j] = (newlong[j] - (inputline.length() -i));
         }  
     }
     printLong();      
}                     

為什么您要證明正確的理由實際上可以按正確的順序輸入?

如果newlong []是全局的,則所有位置都為0

for (int i = inputline.length()-1; i>=0; i--)
{
    char a = inputline.charAt(i);
    String b = "" + a;
    newlong[22 - inputline.length() + i] = Integer.parseInt(b);
}

編輯:如果您更喜歡證明方法,我建議您:

public void rightJustify() {
    System.out.println(inputline.length());

    for (int i = inputline.length()-1; i>=0; i--) {
        newlong[22 - inputline.length() + i] = newlong[i];
        newlong[i] = 0;
    }
}

為什么您在讀取數據時不對齊右邊?

Scanner in = new Scanner(System.in);
    String inputline = in.nextLine();

    StringBuilder inputData = new StringBuilder();
    try {
        for (int i = 0; i < inputline.length(); i++) {
            char a = inputline.charAt(i);
            inputData.append(a);
        }

        for (int i = 0; i < inputData.length(); i++) {
            newlong[newlong.length - inputData.length() + i] = Integer
                    .parseInt("" + inputData.charAt(i));
        }

        printLong(newlong);
    } catch (NumberFormatException nfe) {
        System.out
                .println("NumberFormatException, you must enter a string of digits. "
                        + nfe.getMessage());
    }

這是一個輸出

    123456
    0000000000000000123456

這是解析整數的一種奇怪方法。 盡管如此,您不需要嵌套循環即可對數組進行右對齊:

// len is the number of digits of input.
public void rightJustify(int[] digits, int len)
{
    int off = digits.length() - len;
    for (int i = len; i-- > 0; ) digits[i + off] = digits[j];
    for (int i = off; i-- > 0; ) digits[i] = 0;
}

好的,這顯然是家庭作業,這就是為什么我們使用for循環的原因,但是實際上我只使用:

// len is the number of digits of input.
public void rightJustify(int[] digits, int len) {
    System.arraycopy(digits, 0, digits, len, digits.length() - len);
    System.fill(digits, 0, len, 0);
}

即使功能微不足道,它也沒有理由重新發明輪子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM