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

Java 从字符串中的0重新索引数组,java,Java,如何在Java中转换此结构?(它们是请求参数,因此param:value都是字符串) 要按顺序排列而不留间隙,请执行以下操作: "country[0].town[1].name"="London" "country[0].town[0].name"="Slough" "country[1].town[0].name"="New York" "country[2].name"="UK" 其思想是,所有国家/地区数组元素都将是从[0..x]开始的,没有间隙。那么在每个国家,同样的规则应该适用于城镇

如何在Java中转换此结构?(它们是请求参数,因此param:value都是字符串)

要按顺序排列而不留间隙,请执行以下操作:

"country[0].town[1].name"="London"
"country[0].town[0].name"="Slough"
"country[1].town[0].name"="New York"
"country[2].name"="UK"
其思想是,所有国家/地区数组元素都将是从[0..x]开始的,没有间隙。那么在每个国家,同样的规则应该适用于城镇

我可以按字母数字顺序对这些参数列表进行排序,这样就可以假定它们已排序

我需要这个的原因是:我可以在html表单中创建/删除country对象和towns。如果我创建2个国家,然后删除剩余国家的第一个输入参数(索引),则由于删除了0,剩余国家的第一个输入参数(索引)将为1。当它转到服务器端时,这个国家将映射到列表索引1,并为第一个(索引)创建空的国家对象。它给我带来了问题,我想我可以重新安排输入参数的顺序,这样我就可以避免从请求中自动绑定“空白”列表项

我如何生成输入:


当我删除城镇时,我也会删除输入。因此,当我提交表单时,请求参数中的任何内容都将绑定到java对象结构,缺少的内容将用默认构造函数的元素填充。

我想我应该尝试使用正则表达式来获得解决方案,因为我对正则表达式非常在行。如果您首先按照词汇顺序对它们进行排序,那么这将起作用,前提是您没有没有没有名称的国家

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;
    }
}
公共类RegexCountryParser{
私有静态最终模式townPattern=模式
.compile(“国家\\[\\d*\]\\\.城镇\\[\\d*\]\\.名称”);
私有静态最终模式countryPattern=Pattern.compile(“country\\[\\d*\\]\\\.name”);
私有列表国家=新的ArrayList();
公共void解析(字符串参数、字符串值){
Matcher countryMatcher=countryPattern.Matcher(参数);
Matcher-townMatcher=townPattern.Matcher(参数);
if(countryMatcher.matches()){
国家。添加(新国家(值));
}else if(townMatcher.matches()){
countries.get(countries.size()-1).addTown(newtown(value));
}否则{
抛出新的IllegalArgumentException(“无效请求参数”);
}
}
}
公营国家{
私有字符串名称;
私人名单城镇;
公共国家/地区(字符串名称){
this.name=名称;
this.towns=new ArrayList();
}
公共城市(城镇){
城镇。添加(城镇);
}
@凌驾
公共字符串toString(){
返回名称+“:”+城镇;
}
}
公营城镇{
私有字符串名称;
公共城镇(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
@凌驾
公共字符串toString(){
返回名称;
}
}

直到我开始测试这个,我才意识到你拼写的
country
错了。此外,这只是我的挑剔,但我讨厌看到非复数名称的列表。

我不理解您想要的输出您必须将所有字符串解析为某种数据结构(a
Map
,除非您可以使用类似bean的类),然后从该数据结构输出字符串。如果您有国家[1]国家[7]国家[9]作为字符串,应更改为国家[0]国家[1]国家[2]。当你深入内心的时候也是一样,我可以把它们分类。如果
国家[0]
字符串
那么
国家[0].城镇[11]
怎么可能有法律意义呢?无论如何,如果您的问题是如何编写代码来获取一个数组并返回一个包含空元素的数组,那么可以通过将非空元素添加到
ArrayList
中,然后使用
ArrayList
上的
toArray
方法来返回一个新数组。这不是我需要的。我使用spring框架,它为我将请求参数绑定到对象图。因此,如果请求参数为“countries[0].towns[0]”,它将解析此参数,并使用反射创建新的MyBean().getCountries().set(0,新国家().getTowns().set(0,新城镇))。我喜欢那样。我的问题是,如果在UI中删除一些城镇(假设索引为0)并添加索引为1的新市镇,则在绑定country.getTowns().set(0,null)的框架集null处。这给我带来了麻烦。所以我想在传递到框架绑定器之前,我将“规范化”参数
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;
    }
}