gpt4 book ai didi

java - 数组大小不确定的写入循环

转载 作者:行者123 更新时间:2023-12-02 02:23:13 25 4
gpt4 key购买 nike

下面的代码工作正常。

private int[] list = new int[]{ 3, 5, 10};
private int[] temp = new int[list.length];

public void Generate()
{
for (int a=0;a<list[0];a++)
{
temp[0] = a;
for (int b=0;b<list[1];b++)
{
temp[1] = b;
for (int c=0;c<list[2];c++)
{
temp[2] = c;
int[] asd = temp;
}
}
}
}

但是如果我更改列表数组长度,我必须在内部添加一个 for 循环。如何为任意大小的列表数组编写一种方法?例如:

private int[] list = new int[]{ 2, 5, 12,18};

代码不起作用!

最佳答案

我不是 100% 确定你的函数应该产生什么结果,但如果你想在单个函数中得到与你的代码等效的结果,那么它看起来像这样(假设 C# 作为语言):

private int[] list = new int {3, 5, 10};

public void Generate()
{
List<string> toPrint = new List<string>();
string num_string = string.Empty;
string base_string = string.Empty;
for(int i=list.length; i>-1; i--)
{
num_string = num_string + list[i].ToString();
base_string = base_string +'0';
}
lastnumber = Int16.parse(num_string[list.length - 1]);
int string_length = base_string.length;
bool running = true;
while(running)
{
bool finished = true;
for(int incre=0; incre < lastnumber; incre ++)
{
string temp = base_string;
temp[string_length-1] = (char)incre;
toPrint.add(temp);
}
for(int i = 2; i <= base_string.length; i++ )
{
int currentnumber = Int16.Parse(base_string[base_string.length - i]);
int number_limit = Int16.Parse(num_string[base_string.length -i]));
if(currentnumber < number_limit)
{
currentnumber = currentnumber +1;
base_string[base_string.length - i] = (char)currentnumber
finished = false;
break;
}

if(finished)
{
base_string[base_string.length - i] = 0
}
}

if(finished)
{
running = false;
}

}
string [] asd = toPrint.ToArray()
}

这是您要找的吗?如果没有,您能否提供更多详细信息,以便我获得更好的背景信息?

关于java - 数组大小不确定的写入循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48192614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com