简体   繁体   English

从字符串中的0开始重新索引数组

[英]Re-indexing array from 0 in the string

How could I transform this structure in Java? 如何在Java中转换此结构? (they are request parameters, so both of param:value are strings) (它们是请求参数,因此param:value都是字符串)

"country[0].town[11].name"="London"
"country[0].town[2].name"="Slough"
"country[2].town[3].name"="New York"
"country[3].name"="UK"

to sequential order without gaps like this: 到没有空隙的顺序排列:

"country[0].town[1].name"="London"
"country[0].town[0].name"="Slough"
"country[1].town[0].name"="New York"
"country[2].name"="UK"

The idea is that all country array elements would be from [0..x] without gaps. 这个想法是,所有国家/地区数组元素都应来自[0..x],且没有间隔。 Then in each country the same rule should apply for towns. 然后,在每个国家/地区,相同的规则应适用于城镇。

I can sort the list of these parameters by Alpha-Numeric order so it can be assumed that they are sorted 我可以按字母数字顺序对这些参数的列表进行排序,因此可以假定它们已排序

The story why I might need this: I can create/delete country objects and towns in html form. 我为什么需要这个故事:我可以以html形式创建/删除国家对象和城镇。 If I create 2 countries and then delete the first one input parameter(index) of the remaining country would be 1 since 0 is deleted. 如果我创建2个国家/地区,然后删除其余国家/地区的第一个输入参数(索引)将为1,因为删除了0。 when it goes to server side, this country is being mapped to list index 1 and for the first (index) it creates empty country object. 当转到服务器端时,该国家/地区已映射到列表索引1,并且对于第一个(索引)它创建了一个空的国家对象。 It gives me problems and I thought I could rearrange the order of the input parameters so I could avoid "blank" list items automatically bound from request. 它给我带来了问题,我想我可以重新排列输入参数的顺序,这样就可以避免从请求中自动绑定的“空白”列表项。

How I generate inputs: 我如何生成输入:

when I delete town I delete inputs as well. 当我删除城镇时,我也会删除输入。 So when I submit a form, whatever is in request parameters is bound to the java objects structure and what is missing is populated with the elements with default constructor. 因此,当我提交表单时,请求参数中的所有内容都将绑定到java对象结构,而缺少的内容将使用具有默认构造函数的元素填充。

I figured I'd try to get a solution working using regular expressions, since I'm so terrible at them. 我认为我会尝试使用正则表达式来解决问题,因为我对它们是如此的糟糕。 This will work if you sort them by lexical ordering first, provided that you don't have any countries without names. 如果您没有任何没有名称的国家/地区,那么如果您首先按词汇顺序对它们进行排序,则此方法将起作用。

public class RegexCountryParser {
    private static final Pattern townPattern    = Pattern
                                                    .compile("country\\[\\d*\\]\\.town\\[\\d*\\]\\.name");
    private static final Pattern countryPattern = Pattern.compile("country\\[\\d*\\]\\.name");
    private List<Country>        countries      = new ArrayList<>();

    public void parse(String param, String value) {
        Matcher countryMatcher = countryPattern.matcher(param);
        Matcher townMatcher = townPattern.matcher(param);
        if (countryMatcher.matches()) {
            countries.add(new Country(value));
        } else if (townMatcher.matches()) {
            countries.get(countries.size() - 1).addTown(new Town(value));
        } else {
            throw new IllegalArgumentException("Invalid request parameter");
        }
    }
}

public class Country {
    private String     name;
    private List<Town> towns;

    public Country(String name) {
        this.name = name;
        this.towns = new ArrayList<>();
    }

    public void addTown(Town town) {
        towns.add(town);
    }

    @Override
    public String toString() {
        return name + ": " + towns;
    }
}

public class Town {
    private String name;

    public Town(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name;
    }
}

It wasn't until I started testing this that I realized you spelled country wrong. 直到我开始测试时,我才意识到您拼错了country Also, this is just me being picky but I hate seeing lists that have non-plural names. 另外,这只是我挑剔,但我讨厌看到具有非复数名称的列表。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM