在二维数组上移动数据

时间:2019-01-01 09:33:47

标签: java arrays split

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

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

等等等

3 个答案:

答案 0 :(得分:3)

   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]

答案 1 :(得分:3)

最好创建一个简单的类,而不是像这样基于数组:

class MyObject {

    private String s1;
    private String s2;
    private Integer i1;
    private Integer i2;

    // constructor, getters and setters
}

然后,如果您使用的是Java8 +,则可以使用流式传输,这样会更加有用:

List<MyObject> result = Arrays.stream(array)
        .map(t -> Arrays.stream(t[0].split("\\s(&|and)\\s"))
                        .map(v -> new MyObject(v, t[1], Integer.valueOf(t[2]), Integer.valueOf(t[3])))
                .collect(Collectors.toList())
        ).flatMap(List::stream)
        .collect(Collectors.toList());

对于数组中的每个元素,将第一个t [0]除以&and,然后为每个吐出的元素创建一个新的Object,然后收集结果。

ideone demo


输出如下:

MyClass{s1=John, s2=Car, i1=4324, i2=4944}
MyClass{s1=Jill , s2=Bus, i1=5433, i2=6544}
MyClass{s1= Peter, s2=Bus, i1=5433, i2=6544}
MyClass{s1=Greg, s2=Bus, i1=9384, i2=4329}
MyClass{s1=Jill , s2=Truck, i1=3213, i2=4324}
MyClass{s1= Greg , s2=Truck, i1=3213, i2=4324}
MyClass{s1= Bill, s2=Truck, i1=3213, i2=4324}
MyClass{s1=Mike, s2=Bus, i1=4324, i2=3424}
MyClass{s1=Greg , s2=bus, i1=4324, i2=4334}
MyClass{s1= Lisa , s2=bus, i1=4324, i2=4334}
MyClass{s1= John, s2=bus, i1=4324, i2=4334}

注意:输出基于类中的toString,您可以使用该方法来获取所需的输出,在这种情况下,如果您想要确切的格式,则可以将toString更改为:

@Override
public String toString() {
    return s1 + "," + s2 + "," + i1 + "," + i2;
}

答案 2 :(得分:2)

如果您的数据是CSV文件,则我将首先创建自己的Person类:

public class Person {
    private String name;
    private String vehicle;
    private Integer firstNumber;
    private Integer secondNumber;

    public Person(String name, String vehicle, Integer firstNumber, Integer secondNumber) {
        this.name = name;
        this.vehicle = vehicle;
        this.firstNumber = firstNumber;
        this.secondNumber = secondNumber;
    }

    @Override
    public String toString() {
        return "[" + this.name + ", " + this.vehicle + ", " + this.firstNumber.toString() + ", " + this.secondNumber.toString() + "]";
    }
}

然后,您可以读取CSV文件,并将其解析为Person个对象的列表:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.List;
import java.util.ArrayList;

public class Move {
    private static final String CSV_FILE = "data.csv";
    private static final String CSV_DELIMITER = ",";

    // Store list of objects here
    private static List<Person> fileData = new ArrayList<>();

    public static void main(String[] args) {

        // Parse CSV file
        String line = "";
        try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) {
            while ((line = br.readLine()) != null) {

                // Split data first by delimiter
                String[] data = line.split(CSV_DELIMITER);

                // Split names by & or and
                String[] names = data[0].split("&|and");

                // Add objects to list with correct conversions
                for (String name : names) {
                    String vehicle = data[1];
                    Integer firstNumber = Integer.parseInt(data[2]);
                    Integer secondNumber = Integer.parseInt(data[3]);
                    Person person = new Person(name.trim(), vehicle, firstNumber, secondNumber);
                    fileData.add(person);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Print out objects
        for (Person person : fileData) {
            System.out.println(person.toString());
        }
    }
}

以下哪个来自我们的自定义toString()方法:

[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]

注意:如果您不使用CSV文件,则上述相同的逻辑将起作用。我认为显示文件CSV解析示例也将有所帮助。