在java中创建数组的n对象

在java中创建数组的n对象,java,arrays,Java,Arrays,我在理解如何在Java中创建n对象数组时遇到问题 这是类ServicePath的构造函数,如下所示: public ServicePath(String id) { this.id = id; } 这是我想要创建对象的数组元素 String ServicePathArrays[] = {"SH11","SH13","SH17","SH110","SH111","SH112","SH115", ...} 我尝试了以下操作,但它是手动创建的 ServicePath[] servicePat

我在理解如何在Java中创建
n
对象数组时遇到问题

这是类
ServicePath
的构造函数,如下所示:

public ServicePath(String id) {
   this.id = id;
}
这是我想要创建对象的数组元素

String ServicePathArrays[] = {"SH11","SH13","SH17","SH110","SH111","SH112","SH115", ...}
我尝试了以下操作,但它是手动创建的

ServicePath[] servicePathArray = new ServicePath[ServicePathArrays.length];
例如,它手动创建以下内容

ServicePath[0] = new ServicePath("SH11");
ServicePath[1] = new ServicePath("SH13");
..
..
我想使用
字符串ServicePathArrays
如下所示:

ServicePath[0].id = "SH11";
ServicePath[1].id = "SH12";
ServicePath[2].id = "SH13";
..
..
字符串ServicePathArrays[]={“SH11”、“SH13”、“SH17”、“SH110”、“SH111”、“SH112”、“SH115”和“…”;
ServicePath[]servicePathArray=新ServicePath[servicePathArray.length];
对于(int i=0;i
字符串ServicePathArray[]={“SH11”、“SH13”、“SH17”、“SH110”、“SH111”、“SH112”、“SH115”和“…”;
ServicePath[]servicePathArray=新ServicePath[servicePathArray.length];
对于(int i=0;i
这可以使用jdk8+的功能行为来完成:

String servicePathArray[] = {"SH11", "SH13", "SH17",
                             "SH110", "SH111", "SH112", "SH115"};
List<ServicePath> collection = Stream.of(servicePathArray)
                                     .map(ServicePath::new)
                                     .collect(Collectors.toList());

System.out.println(collection); 
String servicePathArray[]={“SH11”、“SH13”、“SH17”,
“SH110”、“SH111”、“SH112”、“SH115”};
列表集合=Stream.of(servicePathArray)
.map(ServicePath::新建)
.collect(Collectors.toList());
系统输出打印项次(集合);

这可以使用jdk8+的功能行为来完成:

String servicePathArray[] = {"SH11", "SH13", "SH17",
                             "SH110", "SH111", "SH112", "SH115"};
List<ServicePath> collection = Stream.of(servicePathArray)
                                     .map(ServicePath::new)
                                     .collect(Collectors.toList());

System.out.println(collection); 
String servicePathArray[]={“SH11”、“SH13”、“SH17”,
“SH110”、“SH111”、“SH112”、“SH115”};
列表集合=Stream.of(servicePathArray)
.map(ServicePath::新建)
.collect(Collectors.toList());
系统输出打印项次(集合);

您在寻找最简洁的方法吗?您在寻找最简洁的方法吗?使用
Stream.of(servicePathArray)
Arrays.asList(servicePathArray).Stream()更简洁。
。甚至更简洁的是
Stream.of(“SH11”、“SH13”、“SH115”)
。我同意:+1因为这个hintIt的
Stream.of(servicePathArray)
Arrays.asList(servicePathArray).Stream()更简洁。更简洁的是
Stream.of(“SH11”、“SH13”、“SH115”)
。我同意:+1的提示