【问题标题】:Add Int[] array into List<int[]>将 Int[] 数组添加到 List<int[]>
【发布时间】:2014-05-18 05:03:48
【问题描述】:

我在使用 int[] 数组并将它们添加到 List 时遇到问题。我想将我的 int[] 数组的值添加到每个循环中,但是每次我这样做时,我的“某物”对于我添加的每个元素都会获得相同的值。很烦人。我了解数组始终是引用变量。然而,即使是“新”关键词似乎也无济于事。需要发生的是将结果添加到一些枚举对象,如 List 或 Array 或 ArrayList。

这里是代码问题:

给你 N 个计数器,初始设置为 0,你有两种可能的操作:

  • increase(X) - 计数器 X 增加 1,
  • max_counter - 所有计数器都设置为任何计数器的最大值。

给出了一个由 M 个整数组成的非空零索引数组 A。这个数组代表连续的操作:

  • 如果A[K] = X,使得1≤X≤N,那么操作K就是增加(X),
  • 如果 A[K] = N + 1 则操作 K 为 max_counter。

例如,给定整数 N = 5 和数组 A,这样:

A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4

每次连续操作后计数器的值将是:

(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2)

目标是计算所有操作后每个计数器的值。

我从其他人那里复制了一些代码,变量“result”确实正确加载了数据。我只是想把它复制回主程序,这样我就可以看到它了。唯一有效的方法是 += 将其添加到字符串中。因此失去了我可能获得的任何效率。

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

namespace testarray
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] A = new int[7];
            A[0] = 3;
            A[1] = 4;
            A[2] = 4;
            A[3] = 6;
            A[4] = 1;
            A[5] = 4;
            A[6] = 4;
            List<int[]> finish = solution(5, A);

        }
        public static List<int[]> solution(int N, int[] A)
        {
            int[] result = new int[N];
            int maximum = 0;
            int resetlimit = 0;
            int iter = 0;
            List<int[]> collected_result = new List<int[]>;

            for (int K = 0; K < A.Length; K++)
            {
                if (A[K] < 1 || A[K] > N + 1)
                {
                    throw new InvalidOperationException();
                }

                if (A[K] >= 1 && A[K] <= N)
                {
                    if (result[A[K] - 1] < resetlimit)
                    {
                        result[A[K] - 1] = resetlimit + 1;
                    }
                    else
                    {
                        result[A[K] - 1]++;
                    }

                    if (result[A[K] - 1] > maximum)
                    {
                        maximum = result[A[K] - 1];
                    }
                }
                else
                {
                    resetlimit = maximum;
                    result = Enumerable.Repeat(maximum, result.Length).ToArray<int>();

                }

                collected_result.Add(result);

            }
                  //    for (int i = 0; i < result.Length; i++)
                  //result[i] = Math.max(resetLimit, result[i]);

            return collected_result;
        }

    }
}

这不起作用,collected_result 最终结果如下:

(0,0,1,2,0)
(0,0,1,2,0)
(0,0,1,2,0)
(3,2,2,4,2)
(3,2,2,4,2)
(3,2,2,4,2)
(3,2,2,4,2)

我知道是collected_result.Add(result);每次将引用添加到 List 中的每个结果实例。打扰。我试过添加“new”,这是一个编译器错误。最后在绝望中,我只是将所有内容添加到一个很长的字符串中。有人可以帮我弄清楚如何正确加载对象以传回主对象吗?

【问题讨论】:

  • 不要使用奇怪而复杂的解决方案。实现目标的方法不止一种。

标签: c# arrays algorithm


【解决方案1】:

最简单的方法:

在将数组添加到列表之前获取数组的副本:

collected_result.Add(result.ToArray());

【讨论】:

  • 很好,很优雅。我看不出如何停止引用该引用。 ToArray() 方法必须充当一种“新”。我非常感谢您的帮助。
【解决方案2】:

这是一个 Python 解决方案: 定义解决方案(A,N): lenA = len(A) k = 0 最大计数器值 = 0 counters = [0 for x in range(0, N)]

for k in range(0, lenA):
    if A[k] >= 1 and A[k] <= N:
        counters[A[k] - 1] += 1
        max_counter_value = max(counters)
    if A[k] == N + 1:
        counters = [max_counter_value for x in range(0, N)]
    print counters

A = [3, 4, 4, 6, 1, 4, 4] N = 5

解(A,N)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多