简体   繁体   English

如何在java中访问字符串数组的每个元素?

[英]How can I access each element of a string array in java?

I was writing a function to reverse the position of words in a string without reversing the words themselves.我正在编写一个函数来反转字符串中单词的位置,而不反转单词本身。 But I am not able to access the elements of string array.但我无法访问字符串数组的元素。 I am getting error with the following code.以下代码出现错误。

 public static void reverseWords(String sd[]) {
   for(String s : sd){
    System.out.println(s);
   }
}

To reverse the words in a String array do the following.要反转字符串数组中的单词,请执行以下操作。

public class Reverse{
    public static void main(String[] args){

        String[] stringArray = {"Jim", "Jeff", "Darren", "Michael"};

        reverseWords(stringArray);

        for(String strings : stringArray) {
            System.out.println(strings);
        }
    }
    public static void reverseWords(String[] array) {

        for(int i=0; i<array.length/2; i++) {
            String temp = array[array.length-1-i];
            array[array.length-1-i] = array[i];
            array[i] = temp;
        }
    }
}

Output :输出 :

Michael
Darren
Jeff
Jim

To take in a String as an input try the following.要将字符串作为输入,请尝试以下操作。

public class Reverse{
    public static void main(String[] args){

        String sentence = "Bob went to the store";
        String newSentence = reverseWords(sentence);

        System.out.println(newSentence);
    }
    public static String reverseWords(String sentence) {
        //This line splits the String sentence by the delimiter " " or by a space
        String[] array = sentence.split(" ");
        //for loop to reverse the array
        for(int i=0; i<array.length/2; i++) {
            String temp = array[array.length-1-i];
            array[array.length-1-i] = array[i];
            array[i] = temp;
        }

        //These next 4 lines simply take each element in the array
        //and concatenate them all into one String, reversedSentence.
        String reversedSentence = "";
        for(int i=0; i<array.length; i++) {
            //This if statement ensures that no space (" ") is concatenated
            //to the end of the reversedSentence.
            if(i == array.length-1) {
                reversedSentence += array[i];
                continue;
            }
            reversedSentence += array[i] + " ";
        }

        //returning the newly created reversedSentence!
        return reversedSentence;
    }
}

Output :输出 :

store the to went Bob

Hope this helps!希望这可以帮助!

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

相关问题 如何将这个数组的每个元素放入字符串中? - How can I put each element of this array into a string? 如何使用变量访问 Java 数组元素? - how can i access a Java array element by using a variable? 如何在 Java 中为每个循环访问下一个元素? - How do I access the next element in for each loop in Java? 如何创建一个二维数组,每个元素都是 Java 中的一个列表? - How can I create a two dimensional array with each element being a list in Java? 如何为字符串数组中的每个元素添加一个字符串前缀? - How do I prefix a String to each element in an array of Strings? 如何在 Java 中绘制一个字符串数组,它会更改每个循环的内容? - How can I draw a string array in Java which changes contents on each loop? 如何单独访问字符串数组中的每个字符? - How do i access each character individually in a string array? 如何使用Hamcrest检查双精度数组中的每个元素是否“接近”另一个数组中的每个元素? - How can I use Hamcrest to check if each element in an array of doubles is “close” to each element in another array? 如何将字符串集合和每个元素验证为 URL? - How can I validate a collection of string and each element as a URL? 如何在Java中访问数组对象的特定元素? - How do I access a specific element of an object of an array in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM