英文:
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<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))};
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>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
sP
and populate the array
Something like:
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.
}
Your idea was good, but you can't use a loop as a constructor parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论