简体   繁体   English

将arraylist int转换为字符串

[英]Converting arraylist int to string

I am trying to convert an arraylist of integers to an string in reverse. 我正在尝试将整数数组列表反向转换为字符串。 eg (1,2,3,4) converts to "4321". 例如(1,2,3,4)转换为“ 4321”。

However I am unable to get my code to work mainly due to the primitive data type error (basically why you give my an arraylist to do an array thing). 但是,由于原始数据类型错误(基本上是为什么给我一个arraylist来做数组的事情),所以我无法使我的代码正常工作。 My code is currently 我的代码是当前

  public String toString() {
        int n = arrList.size();
        if (n == 0)
            return "0";

        for (int i = arrList.size(); i > 0; i--);
        int nums= arrList[i];
        char myChar = (char) (nums+ (int) '0');
        String result = myChar.toString(arrList);

        return result;
}
  • Your loop had the wrong range and it didn't do anything, since you terminated it with ; 您的循环范围有误,并且没有执行任何操作,因为您以;终止了循环; .
  • In addition, arrList[i] is the way to access an element of an array. 另外, arrList[i]是访问数组元素的方式。 To access an element of an ArrayList you use arrList.get(i) . 要访问ArrayList的元素,请使用arrList.get(i)
  • Finally, you should accumulate the characters / digits somewhere before converting them to a String. 最后,在将字符/数字转换为字符串之前,应先将其存储在某个位置。 I suggest a StringBuilder. 我建议一个StringBuilder。

     StringBuilder sb = new StringBuilder(); for (int i = arrList.size() - 1; i >= 0; i--) { int num = arrList.get(i); sb.append(num); } String result = sb.toString(); 

You have many problems. 你有很多问题。 First, you should remove ; 首先,您应该删除; after the for loop. for循环之后。

Second, you are not concatenating the result, just saving the last value. 其次,您不连接结果,而只是保存最后一个值。

Third, you should loop from the last index, which is arrList.size() - 1 . 第三,您应该从最后一个索引arrList.size() - 1循环。

Fourth, note that the element at place 0 should be counted as well, so you should change your condition to i >= 0 . 第四,请注意,位置0处的元素也应计数,因此您应将条件更改为i >= 0

Finally, accessing arraylist's elements should be done using the method get : arrList.get(i) . 最后,应使用getarrList.get(i)方法访问arraylist的元素。

Now after you understood what your problems are, I would like to suggest a better solution using Java 8: 现在,在您了解了问题所在之后,我想提出一个使用Java 8的更好的解决方案:

Collections.reverse(arrList);
String listString = arrList.stream().map(Object::toString)
                    .collect(Collectors.joining(""));
public String toString() {
        int n = arrList.size();
        if (n == 0)
            return "0";

        String str = "" ; 
        for (int i = arrList.size()-1; i > 0; i--){
        int nums= arrList.get(i);
        str+= nums ; 
        }


        return str;
}

A ListIterator works too - if you dont want indexing. ListIterator也可以工作-如果您不想索引。

ArrayList<Integer> integerList = new ArrayList<Integer>();
StringBuilder sb = new StringBuilder();
ListIterator<Integer> it = integerList.listIterator(integerList.size());


while(it.hasPrevious())
{
    sb.append(Integer.toString(it.previous()));
}

String answer = sb.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM