gpt4 book ai didi

java - 如何在此数组代码上进行 for 循环?

转载 作者:行者123 更新时间:2023-12-01 17:34:38 25 4
gpt4 key购买 nike

我的项目包含此代码以获取此数组中的图片链接。我想用 for 循环创建图像数组。

 private void sliderPic(){
List<String> sP=catalog_data_list.getCatalogImages();
String[] urls;
// Şimdilik bu yöntemle gelen resim sayısına göre dolduruyorum slider'ı.
int spSize = sP.size();
if (spSize==2)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1))};
else if (spSize==1)
urls = new String[]{String.valueOf(sP.get(0))};
else if (spSize==3)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1)), String.valueOf(sP.get(2))};
else if (spSize==4)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1)), String.valueOf(sP.get(2)),
String.valueOf(sP.get(3))};
else if (spSize==5)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1)), String.valueOf(sP.get(2)),
String.valueOf(sP.get(3)), String.valueOf(sP.get(4))};
else if (spSize==6)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1)), String.valueOf(sP.get(2)),
String.valueOf(sP.get(3)), String.valueOf(sP.get(4)), String.valueOf(sP.get(5))};
else if (spSize==7)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1)), String.valueOf(sP.get(2)),
String.valueOf(sP.get(3)), String.valueOf(sP.get(4)), String.valueOf(sP.get(5)), String.valueOf(sP.get(6))};
else if (spSize==8)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1)), String.valueOf(sP.get(2)),
String.valueOf(sP.get(3)), String.valueOf(sP.get(4)), String.valueOf(sP.get(5)), String.valueOf(sP.get(6)),
String.valueOf(sP.get(7))};
else if (spSize==9)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1)), String.valueOf(sP.get(2)),
String.valueOf(sP.get(3)), String.valueOf(sP.get(4)), String.valueOf(sP.get(5)), String.valueOf(sP.get(6)),
String.valueOf(sP.get(7)), String.valueOf(sP.get(8))};
else if (spSize==10)
urls = new String[]{String.valueOf(sP.get(0)), String.valueOf(sP.get(1)), String.valueOf(sP.get(2)),
String.valueOf(sP.get(3)), String.valueOf(sP.get(4)), String.valueOf(sP.get(5)), String.valueOf(sP.get(6)),
String.valueOf(sP.get(7)), String.valueOf(sP.get(8)), String.valueOf(sP.get(9))};

我刚刚尝试使用此代码来创建 for 循环,但它不允许使用语法: if (spSize==2)

      urls = new String[]{for (int i=0;i>3;i++){String.valueOf(sP.get(0)), String.valueOf(sP.get(1))};}

最佳答案

  • 创建一个大小为 spSize 的数组
  • 循环 sP 并填充数组

类似于:

urls = new String[spSize];
for (int i=0; i<spSize; i++) {
urls[i] = sP.get(i) // already a String, no need to call String.valueOf except if sP can contains null values.
}

你的想法很好,但是你不能使用循环作为构造函数参数。

关于java - 如何在此数组代码上进行 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61061876/

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