繁体   English   中英

数组列表 <String> 方法

[英]ArrayList<String> method

我得到了这个代码

public ArrayList<String> inputForMethod_7() {
    ArrayList<String> a = new ArrayList<String>();


    return a; 

到目前为止,我已经创建了数组列表,但是我需要添加字符串,以便在以下情况下将此方法作为参数运行:

public String method_7(ArrayList<String> list) {
    if (list == null || list.size() < 3) { return null; }
    String s = "";
    for (int i=1; i<list.size(); i=i+2) {
        s = s + list.get(i).charAt(i);  
    }
    return s;

这必须返回“是”。

好了,现在我理解了这个问题,解决方案应该如下(我尚未测试过,因此可能需要更多调整):

public ArrayList<String> inputForMethod_7() {
    ArrayList<String> a = new ArrayList<String>();
    String s0 = "whatever"; // i starts at 1, so we will skip the 0th element of the List cause i=1
    String s1 = "xyx"; // we only need the 'y' at position 1, x can be whatever
    String s2 = "whatever"; // we skip this one because i=i+2
    String s3 = "xxxex"; // i=3, that's why we need the 'e' to be in position 3
    String s4 = "whatever"; // we skip this one because i=i+2
    String s5 = "xxxxxsx"; // i=5, that's why we need the 's' to be in position 5

    a.add(s0);
    a.add(s1);
    a.add(s2);
    a.add(s3);
    a.add(s4);
    a.add(s5); 

    return a;

现在像这样调用method_7

String result = method_7(inputForMethod_7());

并根据需要执行结果。

请注意,字符串索引也从0开始,这就是为什么我将所需的字符(“ y”,“ e”和“ s”)放入它们所在的位置的原因。

暂无
暂无

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

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