在循环使用的ArrayList方法一 [英] Looping in ArrayLists with a Method

查看:137
本文介绍了在循环使用的ArrayList方法一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过很多帮助我发展,使字谜的方法,然后将其添加到的ArrayList

With much assistance I have developed a method that makes anagrams and then adds them into an ArrayList.

public void f(String s, String anagram, ArrayList<String> array)
{
    if(s.length() == 0)
    {
        array.add(anagram);
        return;
    }
    for(int i = 0 ; i < s.length() ; i++)
    {
        char l = s.charAt(i);
        anagram = anagram + l;
        s = s.substring(0, i) + s.substring(i+l, s.length());
        f(s,anagram,array);
    }
}

问题是,当我尝试使用此功能,使的ArrayList S IN的循环,增加了字符串取值从一个的ArrayList 到另一个,我得到一个错误,说我不能使用作废,该方法 F()是无效的。

The problem is when I attempt to use this function to make ArrayLists in a loop that adds Strings from one ArrayList to another, I get an error saying I can't use a void, and the method f() is void.

        List<String> Lists = new ArrayList<String>(); //makes new array list
        for(String List : words)
        { //takes values from old array list
            List.trim();
            Lists.add(f(List,"",new ArrayList<String>())); //this is where it doesn't work
        }

让我澄清一次:
我想用这个功能来插入的ArrayList 取值字谜成另一种的ArrayList 每个位置。字谜列表是根据字符串取值正在从一个列表读取到另一个来源。我试图改变的方法静态的,而是不工作,我也去掉了返回; 中的方法一次,但是,这并不要么修复它。

Let me clarify once more: I want to use this function to insert ArrayLists of anagrams into each position in another ArrayList. The anagram Lists are derived from Strings that are being read from one list to another. I tried changing the method to static but that doesn't work, I also removed the return; in the method once, but that doesn't fix it either.

我如何做这件事的工作?

How do I make this whole thing work?

推荐答案

错误发生,因为该方法 F()无效,意思是:它不返回,可以添加到任何值的ArrayList

The error happens because the method f() is void, meaning: it doesn't return any value that can be added to the ArrayList.

调用的答案 F()保存在的ArrayList 作为参数传递给<$ C $通过C>˚F,你应该使用的的ArrayList 来的所有元素添加到列表。事情是这样的:

The answer of invoking f() is stored in the ArrayList passed as a parameter to f, you should probably use that ArrayList to add all of its elements to Lists. Something like this:

List<String> lists = new ArrayList<String>();
for (String list : words) {
    list.trim();
    ArrayList<String> answer = new ArrayList<String>();
    f(list, "", answer);
    lists.addAll(answer);
}

这篇关于在循环使用的ArrayList方法一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆