简体   繁体   English

我如何将数组列表的一个元素打印为大写,而其他元素则为小写

[英]How do i print one element of an arraylist to uppurcase while the other elements lowercase

the elements of array list need to be printed one by one, which one be printed uppsercase other lowercase数组列表的元素需要一一打印,哪一个大写,哪一个小写

public static void main(String[] args) {
    String[] days = {"monday","saturday","tuesday","sunday","friday"};
    ArrayList<String> weekdays = new ArrayList();
    weekdays.add("monday");
    weekdays.add("thursday");
    weekdays.add("wednesday");
    weekdays.add("surtaday");
    weekdays.add("tuesday");
    weekdays.add("sunday");
    weekdays.add("friday");

    // Loop through the ArrayList, printing out "sunday" elements in
    for(int i=0;i<weekdays.size();i++){     
        System.out.println(weekdays.get(i));
    }
}

Can you not add the word to the list as an uppercase letter?您不能将单词作为大写字母添加到列表中吗? If not, depending on which word you want to capitalize, i'm assuming the first one, you can do this:如果没有,根据您要大写的单词,我假设是第一个,您可以这样做:

for(int i=0;i<weekdays.size();i++){

    if(i == 0){
      System.out.println(weekdays.get(i).toUpperCase());
    }else{
        System.out.println(weekdays.get(i));
    }

}

Similarily, if you want to do the second word:同样,如果你想做第二个词:

  for(int i=0;i<weekdays.size();i++){

    if(i == 1){ //change this from 0 to 1
      System.out.println(weekdays.get(i).toUpperCase());
    }else{
        System.out.println(weekdays.get(i));
    }

}

and so on... You need to change the value in the if statement depending on the word you want to capitalize等等...您需要根据要大写的单词更改 if 语句中的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何一个接一个地将元素添加到ArrayList中? - How do i add elements to my ArrayList one after the other? 如何依次访问arrayList中的所有元素? - How do I access all of the elements in an arrayList one after the other? 如何搜索一个列表中的 en 元素是否存在于另一个列表中的元素中,然后打印该元素 - How can I search if en element from one List exists in the elements in the other list and then print this element 如何在不包含null的ArrayList中打印元素? - How do I print elements in an ArrayList without the null? 如何从ArrayList中的数组中打印单个元素 - How do I print a single element from an Array in an ArrayList 如何修改一个 arraylist 而不改变另一个? - How do i modify one arraylist without changing the other? 给定一个包含空白元素的数组,如何打印除空白元素之外的所有元素? - Given a array with a blank element how do I print all elements except the blank one? 如何将59个元素分组为arraylist中的一个元素? - how to group 59 elements into one element in arraylist? 在合并为一个的同时如何对两个ArrayList进行排序? - How do I sort two ArrayList, while merging into one? 当 ArrayList 和构造函数/getter 存储另外两个类时,如何从一个类添加到 ArrayList - How do I add to an ArrayList from one class, when the ArrayList and constructors/getters are stored 2 other classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM