简体   繁体   English

转换清单 <Object> 到String返回空结果

[英]Converting List<Object> to String returns empty results

Disclaimer : I'm using this Post , as reference for List<Object> to List<String> and this Post for Java List<String> of strings to a JavaScript array. 免责声明 :我使用此Post ,作为List<Object>List<String>引用,以及此Post的Java List<String>字符串到JavaScript数组的引用。

I've List<Seat> and I want to get all values of it in a comma separated String , I tried in this way 我有List<Seat> ,我想用逗号分隔的String来获取它的所有值,我以此方式尝试过

import java.util.*;
import java.lang.*;

class Rextester
{  

    public Rextester(){
        Seat seat1 = new Seat();
        seat1.setSeatNumber(1);

        Seat seat2 = new Seat();
        seat2.setSeatNumber(2);

        Seat seat3 = new Seat();
        seat3.setSeatNumber(3);


        List<Seat> seatList = new ArrayList<Seat>();
        seatList.add(seat1);
        seatList.add(seat2);
        seatList.add(seat3);
        Utility util = new Utility();
        String stringSeats = util.toJavascriptArray(seatList);
        System.out.println("JavaScriptArray is " + stringSeats);
    }

    public static void main(String args[])
    {
        new Rextester();

    }



    private class Seat {

        private Integer seatNumber;        

        public Integer getSeatNumber() {
            return this.seatNumber;
        }

        public void setSeatNumber(Integer seatNumber) {
            this.seatNumber = seatNumber;
        }   
        public String toString() {
            return ""+ seatNumber;
        }
    }

    private class Utility {

        public String toJavascriptArray(List<Seat> listSeats){
            List<String> strings = new ArrayList<String>();
            for (Seat object : listSeats) {
                strings.add(object != null ? object.toString() : null);
            }
            String[] arr = new String[strings.size()];
            arr = strings.toArray(arr);
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            for(int i=0; i<arr.length; i++){

                if(i+1 < arr.length){
                    sb.append(",");
                }
            }
            sb.append("]");
            return sb.toString();
        }
    }
}

but this gives me 但这给了我

JavaScriptArray is [,,]

on console, am I making some mistakes? 在控制台上,我是否犯了一些错误? an online working code is http://rextester.com/NDUGT61105 在线工作代码是http://rextester.com/NDUGT61105

You didn't append iterated element, see below 您没有附加迭代元素,请参见下文

        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]); // add this
            if (i + 1 < arr.length) {
                sb.append(",");
            }
        }

have a look at the toString implementations of Arrays and List 看看ArraysListtoString实现

Simply you can return 只需返回即可

strings.toString()

or 要么

Arrays.toString(arr)

To get the expected result 获得预期的结果

Another option, if you're using Java 8 is using StringJoiner: 另一个选择,如果您使用的是Java 8,则使用StringJoiner:

https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html

You forgot to append the array elements to the string buffer! 您忘记了将数组元素附加到字符串缓冲区! Add

sb.append(arr[i]);

inside the loop where you prepare the string buffer and everything is ok 在准备字符串缓冲区的循环内,一切正常

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

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