【问题标题】:Convert int array to directions将 int 数组转换为方向
【发布时间】:2019-05-26 22:53:03
【问题描述】:

我有一个代码 sn-p(参见下面的 sn-p),它生成如下数组:[0, 3, 1, -2, 0, -1, 1, 1, -2]。这里的 int 数字代表从一个位置到另一个位置的运动。我想将数值转换为表示从 0 开始的方向的文本。正数表示向东的步数 - 所以数字 3 将被翻译成“eee”,数字二是“ee”等等。负值表示相反的直接向西的步骤,因此 -2 将显示为 ww,依此类推。任何运动都不应表示为 0。

我对这一切都很陌生,不确定如何从数组中获取值并将它们转换为如上所述的指令。

下面的代码显示了整数数组是如何生成的——从上一个位置减去下一个位置以获得它们之间的步数。

int [] differenceX = new int [noOfRecordsX];
differenceX [0] = 0;

for( int i=0; i < noOfRecordsX -1 ;i++)
{
   differenceX [i+1]= inputX [i+1] - inputX[i];
}


从这里我想生成描述各个方向的步骤的文本,以便这个数组:

[0, 3, 1, -2, 0, -1, 1, 1, -2]

将被转换成这个字符串:

0,eee,e,ww,0,w,e,e,ww

【问题讨论】:

  • “differenceX”是您所指的产生输出“[0, 3, 1, -2, 0, -1, 1, 1, -2]”的数组吗?换句话说,您想将 "[0, 3, 1, -2, 0, -1, 1, 1, -2]" 翻译成一个字符串,看起来像 "0,eee,e,ww,0,w, e,e,ww”?
  • 据我了解,源数组包含 POSITIONS,我们必须输出它们之间的 MOVEMENTS。 Barns 了解您已经在数组中进行了移动,并希望将它们转换为字符串。解释得更好。
  • @Gangnus :: 我觉得不错!!
  • @Barns 在我的版本之后,确实如此。按类型描述任务中的输入值是一个概念错误。它们应该被定义,而不是通过表示(实际上这不是任务设置者的业务),而是通过它们的角色
  • 数组生成器是错误的 - 它永远不会填补差异X[0]

标签: java string loops int


【解决方案1】:

您可以使用以下代码来做到这一点:

int arr[] = { 0, 3, 1, -2, 0, -1, 1, 1, -2 };

for (int i = 0; i < arr.length; i++) {
    if (arr[i] < 0) { // west
        for (int j = arr[i]; j < 0; j++) {
            System.out.println("w");
        }
    } else if (arr[i] > 0) { // east
        for (int j = 0; j < arr[i]; j++) {
            System.out.println("e");
        }
    }
}
  • 如果该数字为负数,则我们从该值迭代到 0。
  • 如果该数字为正,则我们从 0 迭代到该值。

【讨论】:

  • 感谢您的回答,效果很好。我将如何更改打印并将所有方向放入 textView?
  • 不确定,但也许这个link 可以帮助你。
【解决方案2】:

如果你想取回字符串而不是仅仅写入控制台,试试这个:

private void testMyMethod(){
    String resultString = "";
    int[] array = { 0, 3, 1, -2, 0, -1, 1, 1, -2 };

    for(int step : array){
        String direction = convertToDirection(step);
        // Adding a comma -- as you requested
        // just add this in case you what to indicate a start point ==> X
        if(direction.isEmpty()){
            resultString = resultString.concat("X");
        }
        else{
            resultString = resultString.concat(direction);
        }
        resultString = resultString.concat(",");
    }
    resultString = resultString.subString(0, resultString.length()-1);
    myTextView.setText(resultString);
}

private String convertToDirection(int step){
    String direction = "";

    if(step > 0){
        direction = "e";
    }
    else if(step < 0){
        direction = "w";
    }

    String result = "";
    int len = Math.abs(step);

    for(int i = 0; i < len; i++){
        result = result.concat(direction);
    }

    return result;
}




编辑:
一个不太冗长的解决方案:

private void testMyMethod(){
    int[] array = { 0, 3, 1, -2, 0, -1, 1, 1, -2 };
    StringBuilder sb = new StringBuilder();
    for(int step : array){
        sb.append(convertToDirection(step).concat(","));
    }
    // Remove the last ","
    sb.deleteCharAt(sb.length()-1);
    myTextView.setText(sb.toString());
}

private String convertToDirection(int step){
    if(step == 0) return "0";
    String direction = step > 0 ? "w" : "e";

    int len = Math.abs(step);
    return new String(new char[len]).replace("\0", direction);
}


借用这个:new String(new char[len]).replace("\0", direction);
从此解决方案: Repeat String

【讨论】:

  • 感谢您的回答,效果很好。我如何将每个结果用逗号分开,例如eee, e, ww, w, e, e, ww
  • 只需在从convertToDirection()testMyMethod()for 循环返回的字符串末尾添加一个逗号。
  • 你在哪里计算差异?
  • @Gangnus :: 我不明白?有什么区别? OP 表示他们只想将 array 翻译成字符串。
  • 据我了解,源数组包含 POSITIONS,我们必须输出它们之间的 MOVEMENTS。所以,你必须减去。我会请提问者解释。
【解决方案3】:

我们应该更好地使用字符重复而不是循环。查看Simple way to repeat a String in java 了解不同的方式。

int arr[] = { 0, 3, 1, -2, 0, -1, 1, 1, -2 };
StringBuilder output = new StringBuilder();
for(int step : array){
    int length = Math.abs(step);
    if (step < 0) { // west
        output.append(new String(new char[length]).replace("\0", "w"));
    } 
    else if (step > 0) { // east
        output.append(new String(new char[length]).replace("\0", "e"));
    }
    else  output.append("0");
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多