如何将列表从一种方法返回到另一种方法. [英] How do I return a list from one method to another method.

查看:143
本文介绍了如何将列表从一种方法返回到另一种方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者.我正在尝试编写代码以打印给定字符串的所有可能排列.我已经使用一个列表来存储所有可能的排列.我无法从另一个方法将列表返回给Main()方法.请帮助我理解代码中的问题.

我尝试过的事情:

Hi I am a beginner to c#. I am trying to write a code to print all possible permutations of a given string. I have used a List to store all the possible permutations. I am unable to return the list to Main() method from another method. Please help me to understand the problem in my code.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        string a = Console.ReadLine();
        List<string> b = new List<string>();
        b = Permutation.Perm("", a);
        foreach(string i in b)
        {
           Console.WriteLine(i); 
        }
        Console.ReadLine();
    }
}
public class Permutation
{
    public static List<string> Perm(string a,string b)
    {
        List<string> c = new List<string>();
        int l = b.Length;
        if(l==0)
        {
            c.Add(a);
        }
        else
        {
            for (int i = 0; i < l; i++)
            {
                string d = b.Substring(0, i) + b.Substring(i+1,l - (i + 1));
                Perm(a + b[i], d);
            }
        }
        return c;
    }
}

推荐答案

显示的代码应该起作用,但前提是您将字符串添加到列表中.不需要-因为您的方法是递归的,并且您将字符串添加到列表的当前版本中并返回它,但是当递归调用退出时,您对返回值不执行任何操作.
如果您改为执行以下操作,则会改变这种情况:
The code you show should work, but only if you add strings to your list. Which you don''t - because your method is recursive, and you add your strings to the current version of the list and return it, but you don''t do anything with that return value when your recursive call exits.
That would change if you did this instead:
c.AddRange(Perm(a + b[i], d));


在您的Permutation方法中,您将创建一个新实例List< string>将结果添加到结果中并返回结果,因此,每次在排列"中添加某些内容时,都会将其添加到新的新列表中,这样您就只会得到最后添加的内容.不必每次都将当前列表传递到Permutation函数中,以便对其进行修改,而不是每次都创建一个新列表.

In your Permutation method you create a new instance of List<string> to add the results to and return it, so every time you add something in Permutation you are adding it to a fresh new list so you only ever get the last thing added. Rather than creating a new List each time you should pass your current list into the Permutation function so it can be amended

class Program
{
    static void Main()
    {
        string a = Console.ReadLine();
        List<string> b = new List<string>();
        b = Permutation.Perm("", a, b);
        foreach (string i in b)
        {
            Console.WriteLine(i);
        }
        Console.ReadLine();
    }
}

public class Permutation
{
    public static List<string> Perm(string a, string b, List<string> c)
    {
        int l = b.Length;
        if (l == 0)
        {
            c.Add(a);
        }
        else
        {
            for (int i = 0; i < l; i++)
            {
                string d = b.Substring(0, i) + b.Substring(i + 1, l - (i + 1));
                c = Perm(a + b[i], d, c);
            }
        }
        return c;
    }
}



但是,只做这样的事情会更简单



However it would be simpler to just do something like this

class Program
{
    static void Main()
    {
        string a = Console.ReadLine();
        List<string> b = new List<string>();
        Permutation.Perm("", a, b);
        foreach (string i in b)
        {
            Console.WriteLine(i);
        }
        Console.ReadLine();
    }
}

public class Permutation
{
    public static void Perm(string a, string b, List<string> c)
    {
        int l = b.Length;
        if (l == 0)
        {
            c.Add(a);
        }
        else
        {
            for (int i = 0; i < l; i++)
            {
                string d = b.Substring(0, i) + b.Substring(i + 1, l - (i + 1));
                Perm(a + b[i], d, c);
            }
        }
    }
}



这利用了一个事实,即您的列表是内存中的对象,因此您无需在更新后真正返回它,排列功能仅需要对其进行引用,并且添加到列表中的任何项目对于引用该列表的任何变量,在您的Main函数中包括"b".

您还应该为变量选择更好的名称,您已经可以看到命名方案会引起混乱,并且"a","b"和"c"在不同的函数中表示不同的含义.



This takes advantage of the fact that your List is an object in memory so you don''t really need to return it after it has been updated, the Permutation function simply needs a reference to it and any item added to the list is visible to any variable referencing that list which includes "b" in your Main function.

You should also choose better names for your variables, you can already see your naming scheme will cause confusion and "a" "b" and "c" mean different things in different functions.


这篇关于如何将列表从一种方法返回到另一种方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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