令牌“."上的语法错误,@预期在此令牌之后 [英] Syntax error on token ".", @ expected after this token

查看:64
本文介绍了令牌“."上的语法错误,@预期在此令牌之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class ListFile {
    public static void main(String[] args){
        String[] arr = {"text", "tekl"};
        List<String> list = Arrays.asList(arr);     
        List<String> listt = Arrays.asList({"text", "tttt"});
        }
}

第4行完全正常.但是,第5行在第36列给出错误:令牌."的语法错误,@预期在此令牌之后.参数是否以 {"text","tttt"} 的形式被视为此处的块?

Line 4 is totally working fine. However, line 5 gives error: "Syntax error on token ".", @ expected after this token" at column 36. Is the argument passed as {"text", "tttt"} is considered as block here?

推荐答案

当您执行 Type [] arr = {…,…}; 时,这是).

When you do Type[] arr = { …, … }; that's an array initializer. It can only be used in array declarations (or in array creation expressions, i.e. new String[]{"a", "b"}).

Arrays.asList 定义为采用varargs参数( asList(T ... a)),因此您不必先将参数包装在数组中: Arrays.asList("text","tek1")已经根据您的参数隐式创建了一个数组,并将其传递给方法.

Arrays.asList is defined to take varargs arguments (asList(T... a)), so you do not have to wrap your arguments in an array first: Arrays.asList("text", "tek1") will already implicitely create an array from your arguments and pass this to the method.

这篇关于令牌“."上的语法错误,@预期在此令牌之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆