[英]ArrayList<Objects[]> to strings to use in SQL IN statement
I am trying to build a string to pass it as an SQL query within the IN statement. 我正在尝试构建一个字符串以将其作为IN语句内的SQL查询传递。
ArrayList<Object[]> arrayList = new ArrayList<Object[]>();
List<String> strings = new ArrayList<>(arrayList .size());
for (Object object : arrayList ) {
strings.add(Objects.toString(object, null));
}
System.out.println("askldnlkasdn"+strings);
This still prints out the memory locations instead of the actual string 这仍然会打印出内存位置,而不是实际的字符串
askldnlkasdn[[Ljava.lang.Object;@7bb11784, [Ljava.lang.Object;@33a10788, [Ljava.lang.Object;@7006c658, [Ljava.lang.Object;@34033bd0, [Ljava.lang.Object;@47fd17e3, [Ljava.lang.Object;@7cdbc5d3, [Ljava.lang.Object;@3aa9e816, [Ljava.lang.Object;@17d99928, [Ljava.lang.Object;@3834d63f, [Ljava.lang.Object;@1ae369b7]
I have also tried out using StringBuilder and StringUtils. 我也尝试过使用StringBuilder和StringUtils。 But things dont seem to work. 但是事情似乎没有用。
Any inputs as to where the problem is? 关于问题出在哪里有什么投入吗?
您应该在对象中覆盖方法toString
The problem you have is that you are implicitly using the toString()
method of the Object
elements inside your ArrayList
. 您遇到的问题是您隐式使用了ArrayList
内Object
元素的toString()
方法。 By default, that method returns the class and address of the Object
. 默认情况下,该方法返回Object
的类和地址。 You should override the toString()
method in every class you will use inside the list so it returns what you want it to. 您应该在列表中使用的每个类中重写toString()
方法,以便它返回您想要的内容。
You can just save it as String
, like this code 您可以将其保存为String
,例如以下代码
ArrayList<String> arrayList = new ArrayList<>();
List<String> strings = new ArrayList<>(arrayList .size());
for (Object object : arrayList ) {
strings.add(Objects.toString(object, null));
}
System.out.println("askldnlkasdn"+strings);
Or you want it Object for specific purpose? 还是您想要对象用于特定目的?
This is new code that may help, 这是新的代码,可能会有所帮助,
// Data of Array of Object for test the Code
Object[] a = new Object[1];
a[0] = "Hello";
Object[] b = new Object[1];
b[0] = "Friend";
Object[] c = new Object[1];
c[0] = "This is";
Object[] d = new Object[1];
d[0] = "Just Test";
// The Array List of objects and the data entry
ArrayList<Object[]> arrayList = new ArrayList<Object[]>();
arrayList.add(a);
arrayList.add(b);
arrayList.add(c);
arrayList.add(d);
// New List of strings
List<String> strings = new ArrayList<>(arrayList .size());
// The Process of adding the data from array list of objects to the strings
for(int i = 0; i < arrayList.size(); i++){
strings.add((String) arrayList.get(i)[0]);
}
// Just for print the data to console
for(int i = 0 ; i < strings.size(); i++){
System.out.println(strings.get(i));
}
System.out.println("askldnlkasdn "+strings.get(0));
I hope that solve the problem, if not please inform me, you can use it for more than one dimensional array . 我希望解决该问题,如果不能解决,请通知我,您可以将其用于多个一维数组 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.