Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 功能建议的Var列表_Java_Reverse - Fatal编程技术网

Java 功能建议的Var列表

Java 功能建议的Var列表,java,reverse,Java,Reverse,我做了一个简单的程序来反转列表。现在我试图反转一个字符串,所以我把它转换成一个字符数组,并将它赋给一个变量charz。但是我的函数只接受一个字符串列表 现在在python中,我可以有一个多变量类型的列表,比如a=[1,hello,1.3],这不会是一个问题。有没有同样的方法来创建这样的列表,以便我可以在函数中使用它?如何让它接受这个变量charz,而不只是为这个特定的数据类型生成一个新函数 package test_proj1; import java.util.Arrays; import

我做了一个简单的程序来反转列表。现在我试图反转一个字符串,所以我把它转换成一个字符数组,并将它赋给一个变量charz。但是我的函数只接受一个字符串列表

现在在python中,我可以有一个多变量类型的列表,比如a=[1,hello,1.3],这不会是一个问题。有没有同样的方法来创建这样的列表,以便我可以在函数中使用它?如何让它接受这个变量charz,而不只是为这个特定的数据类型生成一个新函数

package test_proj1;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class TestClass1
{
    static Scanner reader = new Scanner(System.in);

    public static void main (String[] args)
    {
        List<String> sent = new ArrayList<String>(Arrays.asList("Jake","you","are"));
        System.out.println("Without reversal the inuilt list is "+sent);
        sent = lisrev(sent);
        System.out.println("After reversal the list is "+ sent);
        System.out.println("\nNow then enter a word for me: ");
        String word = reader.nextLine();
        char[] charz = word.toCharArray();
        // List<String> revword = lisrev(charz);  
        // I want the above line to work but it won't because my function
        // lisrev will only accept  a list of type String




    }
    public static List<String> lisrev (List<String> lis)
    {
        int lastindex = lis.size() - 1;
        List<String> newlis = new ArrayList<String>();


        while (lastindex != -1)
        {
            String elem = lis.get(lastindex);
            newlis.add(elem);
            lastindex -= 1;
        }


        return newlis;
    }

}
结果将是,例如:

Without reversal the inuilt list is [Jake, you, are]
After reversal the list is [are, you, Jake]

Now then enter a word for me: 
Hello My name is Sam, How are you today?
?yadot uoy era woH ,maS si eman yM olleH
List<Object> sent = new ArrayList<Object>(Arrays.asList("Jake","you","are", 20, "years", "old", "and", 70.3, "inches", "high"));
for (int i = 0; i < sent.size(); i++) {
    System.out.println(sent.get(i).toString());
}
此外,如果你想逆转用户的一句话,你可以这样做:

List<String> theWord = new ArrayList<String>();
String eachWord="";
for(int i=0; i<charz.length; i++){
    eachWord+=charz[i]; // append the char to the String
    if(charz[i]==' ' || i==charz.length-1){
       if(!eachWord.replace(" ", "").equals("")){ // for the followed spaces
          theWord.add(eachWord.replace(" ", "")); // add the word 
       }
       eachWord = ""; // start new string
    }
}
System.out.println(lisrev(theWord)); 

如果希望列表包含几种不同的数据类型,请将列表声明为对象,例如:

Without reversal the inuilt list is [Jake, you, are]
After reversal the list is [are, you, Jake]

Now then enter a word for me: 
Hello My name is Sam, How are you today?
?yadot uoy era woH ,maS si eman yM olleH
List<Object> sent = new ArrayList<Object>(Arrays.asList("Jake","you","are", 20, "years", "old", "and", 70.3, "inches", "high"));
for (int i = 0; i < sent.size(); i++) {
    System.out.println(sent.get(i).toString());
}
如果希望列表中的双精度或整数类型为字符串,则可以执行以下操作之一:

对于整数、双精度、浮点等:

String age = sent.get(3).toString(); 


您可以编写一个在泛型列表上工作的函数。看啊,这就是我要找的,非常有意义谢谢:嗯,非常感谢:现在有意义谢谢你用一个例子详细解释,非常感谢。
String age = sent.get(3).toString(); 
String age = String.valueOf(sent.get(3));