如何在这个数组代码中使用for循环?

huangapple go评论78阅读模式
英文:

How can make for loops on this array code?

问题

以下是您要求的翻译好的代码部分:

private void sliderPic(){
    List<String> sP = catalog_data_list.getCatalogImages();
    String[] urls;
    
    int spSize = sP.size();
    if (spSize >= 1 && spSize <= 10) {
        urls = new String[spSize];
        for (int i = 0; i < spSize; i++) {
            urls[i] = String.valueOf(sP.get(i));
        }
    }
}

请注意,我已经将原始代码中的一系列 if-else 分支重构为一个简单的循环结构,以便根据 sP 列表的大小创建对应数量的图片链接数组。

英文:

My project include this code to get picture links in this array. I want to create images array with for loops.

 private void sliderPic(){
  List&lt;String&gt; sP=catalog_data_list.getCatalogImages();
  String[] urls;
      // Şimdilik bu y&#246;ntemle gelen resim sayısına g&#246;re dolduruyorum slider&#39;ı.
   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))};

I just tried this code to make for loop but it don't allow in syntax:
if (spSize==2)

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

答案1

得分: 0

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

类似于:

urls = new String[spSize];
for (int i=0; i<spSize; i++) {
    urls[i] = sP.get(i); // 已经是字符串,除非 sP 可能包含空值,否则不需要调用 String.valueOf
}

你的想法是不错的,但是不能将循环用作构造函数的参数。

英文:
  • Create an array of size spSize
  • Loop on sPand populate the array

Something like:

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

Your idea was good, but you can't use a loop as a constructor parameter.

huangapple
  • 本文由 发表于 2020年4月6日 22:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定