gpt4 book ai didi

java - 在二维数组上移动数据

转载 作者:行者123 更新时间:2023-11-29 10:08:44 25 4
gpt4 key购买 nike

我有一个二维数组,布局如下:

John,Car,4324,4944
Jill & Peter,Bus,5433,6544
Greg,Bus,9384,4329
Jill & Greg and Bill,Truck,3213,4324
Mike,Bus,4324,3424
Greg & Lisa & John,bus,4324,4334

基本上,在某些情况下,文本的第一部分由“and”分隔,在其他情况下由“&”分隔。有时数据甚至同时使用两者。

数组上方第一行的布局示例是:

Array[0][0] = John
Array[0][1] = Car
Array[0][2] = 4324
Array[0][3] = 4944

我对如何处理这段代码一无所知,但我要求每个名称都在单独的行中,但其后的所有数据都必须相同。

所以上面的数组会变成:

John,Car,4324,4944
Jill,Bus,5433,6544
Peter,Bus,5433,6544
Greg,Bus,9384,4329
Jill,Truck,3213,4324
Greg,Truck,3213,4324
Bill,Truck,3213,4324
Mike,Bus,4324,3424
Greg,Bus,4324,4334
Lisa,Bus,4324,4334
John,Bus,4324,4334

因此,对于上面的示例,数组将是:

Array[0][0] = John
Array[0][1] = Car
Array[0][2] = 4324
Array[0][3] = 4944

Array[1][0] = Jill
Array[1][1] = Bus
Array[1][2] = 5433
Array[1][3] = 6544

Array[2][0] = Peter
Array[2][1] = Bus
Array[2][2] = 5433
Array[2][3] = 6544

等等等等

最佳答案

   public static void main(String[] args) {
List<String[]> datas = new ArrayList<String[]>();
datas.add("John,Car,4324,4944".split(","));
datas.add("Jill & Peter,Bus,5433,6544".split(","));
datas.add("Greg,Bus,9384,4329".split(","));
datas.add("Jill & Greg and Bill,Truck,3213,4324".split(","));
datas.add("Mike,Bus,4324,3424".split(","));
datas.add("Greg & Lisa & John,bus,4324,4334".split(","));
datas.add("Greg & roland & John,bus,4324,4334".split(","));

for (String[] data : datas) {
if(data[0].contains("&") || data[0].contains(" and ")) {
String[] names = data[0].split("&|(\\sand\\s)");
for (String name : names) {
data[0] = name.trim();
System.out.println(Arrays.toString(data));
}
}else {
System.out.println(Arrays.toString(data));
}
}
}

输出

[John, Car, 4324, 4944]
[Jill, Bus, 5433, 6544]
[Peter, Bus, 5433, 6544]
[Greg, Bus, 9384, 4329]
[Jill, Truck, 3213, 4324]
[Greg, Truck, 3213, 4324]
[Bill, Truck, 3213, 4324]
[Mike, Bus, 4324, 3424]
[Greg, bus, 4324, 4334]
[Lisa, bus, 4324, 4334]
[John, bus, 4324, 4334]
[Greg, bus, 4324, 4334]
[roland, bus, 4324, 4334]
[John, bus, 4324, 4334]

关于java - 在二维数组上移动数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53994378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com