简体   繁体   English

如何使用Java创建逗号分隔的数组数组

[英]How can I create an array of comma separated arrays using java

I am trying to use the google maps API to list multiple locations in a web page using my Java Spring MVC Web Application . 我正在尝试使用google maps API使用Java Spring MVC Web Application在网页中列出多个位置。 The locations are stored in my database. 这些位置存储在我的数据库中。 Now I am trying to return a list of locations in the following format: 现在,我尝试以以下格式返回位置列表:

['ABC', -33.890542, 151.274856],
      ['Coogee Beach', -33.923036, 151.259052],
      ['Cronulla Beach', -34.028249, 151.157507],
      ['Manly Beach', -33.80010128657071, 151.28747820854187],
      ['Maroubra Beach', -33.950198, 151.259302]

I need this format since the locations is called as follows in the script: 我需要这种格式,因为在脚本中按以下方式调用位置:

var locations = [
      ['ABC', -33.890542, 151.274856],
      ['Coogee Beach', -33.923036, 151.259052],
      ['Cronulla Beach', -34.028249, 151.157507],
      ['Manly Beach', -33.80010128657071, 151.28747820854187],
      ['Maroubra Beach', -33.950198, 151.259302]
    ];

What I am trying to do is to create this arrays using Java and then pass it to the UI. 我正在尝试使用Java创建此数组,然后将其传递给UI。 I am not sure how to create such an array structure using Java. 我不确定如何使用Java创建这样的数组结构。

the array contains items of different data types. 该数组包含不同数据类型的项目。 this creates a problem in statically typed languages like Java. 这会在诸如Java之类的静态类型语言中造成问题。 the array will have to be defined Object[] which is problematic since the compiler will not be able to help you populate the array properly. 必须将数组定义为Object[] ,这是有问题的,因为编译器将无法帮助您正确填充数组。

You should design the API between the server and client to use array of Json objects. 您应该在服务器和客户端之间设计API以使用Json对象数组。 something like 就像是

[
  {"Name": "Coogee Beach", "longitude":-33.923036, "latitude":151.259052},
  {"Name": "Cronulla Beach", "longitude":-34.028249, "latitude":151.157507},
  ...
]

this is best represented in Java by creating a class with properties that match the JSON object: 在Java中,最好通过创建一个具有与JSON对象匹配的属性的类来表示:

public class Place {
  public String name;
  public double longitude;
  public double latitude;
}

you should create an array (or List ) of PLace s and you can use one of the many Jason libraries that map Java objects to-from Json objects. 您应该创建一个PLace的数组(或List ),并且可以使用许多Jason库之一来将Java对象映射到Json对象。

The design of returning lat/long in array doesn't look great, but here's the solution that should work. 返回纬度/经度数组的设计看起来不太好,但是这是应该起作用的解决方案。

Object[][] result = new Object[list.size()][3];
for (int i = 0; i < list.size; i++) {
    result[i][0] = list[i].locName;
    result[i][1] = list[i].latitude;
    result[i][2] = list[i].longitude;
}
return result;

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

相关问题 如何将逗号分隔的值与数组相加? 使用java脚本 - how can i get the values separated by a comma to a array? using java script 如何创建一个只允许逗号分隔数字的正则表达式? - How can I create a Regex that only lets in comma separated numbers? 如何在JavaScript中创建以逗号分隔的ID列表? - How can I create a comma-separated list of IDs in JavaScript? 如何将逗号分隔的查询字符串转换为字符串数组 - How can i convert comma separated query String to a Array of Strings 如何将逗号分隔的字符串转换为数组? - How can I convert a comma-separated string to an array? 如何将嵌套的数组数组转换为逗号分隔的字符串 - how to convert nested array of arrays into comma separated string 如何将逗号分隔的字符串数组分隔成各种数组? - How to separate array of comma separated string into various arrays? 逗号分隔的字符串成多个 arrays 用逗号分隔,使用 javascript - Comma separated string into multiple arrays separated by comma using javascript 我可以将.map数组转换为带双引号的逗号分隔数组吗? - Can I turn .map array into a comma separated array with double quotes? 如何从逗号分隔的字符串创建数组,将其从低到高排序,然后将其重新连接成逗号分隔的字符串? - How create array from comma separated string, sort it low to high, then join it back into comma separated string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM