这个java代码有什么问题

时间:2015-11-30 20:20:51

标签: java

private static final List<String> datas = new List<String>() {{
    add("aaaa");
    add("bbbb");
    System.out.println(datas);
}};

我已经声明了一个列表并添加了一些数据。然后我想打印存储在该列表中的数据。但代码不起作用。你能解释一下原因吗?

3 个答案:

答案 0 :(得分:1)

您在这里使用的是双括号初始化。基本上,这会创建一个匿名类,其初始化程序会执行一些处理,例如将数据添加到列表中。

使用添加的换行符编写,这就是它的真实情况:

private static final List<String> datas = new List<String>() {

    {
        // this is the initializer block
        add("aaaa");
        add("bbbb");
        System.out.println(datas);
    }

    // huh? no methods of List are implemented here!

};

第一个问题是你正在尝试创建一个List的匿名类,但是你没有覆盖它的任何抽象方法。这会导致编译错误。

第二个“问题”是System.out.println类在初始值设定项中,但是此时变量datasnull,因此将打印(并且这是可能不是你想要的。)

首先,你想要的是创建一个派生自ArrayList的匿名类,或者其他一些列表实现,这样你就不必重写任何方法。其次,您不希望在初始化程序内打印变量的内容,而是在其外部。第三,也许是最重要的:你根本不想使用双括号初始化

答案 1 :(得分:0)

您需要实现java.util.List接口的方法。并且您的代码不在方法或静态块中。

我认为这更容易

%s

答案 2 :(得分:-1)

您的代码将List接口实现为匿名类,因此您必须实现所有List方法。我认为你想到的静态列表初始化应该像:

GET http://www.tvmagazine.com.br/guia/programacao,NET,0 HTTP/1.1    
Accept-Language: pt-BR    
Accept: */*    
Accept-Encoding: gzip, deflate    
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko    
Host: www.tvmagazine.com.br    
DNT: 1    
Connection: Keep-Alive    
Pragma: no-cache


HTTP/1.1 200 OK    
Connection: close    
Date: Tue, 01 Dec 2015 09:23:36 GMT    
Server: Microsoft-IIS/6.0    
X-Powered-By: ASP.NET    
P3P: CP="CAO PSA OUR"    
P3P: CP="CAO PSA OUR"    
Content-Length: 345726    
Content-Type: text/html    
Set-Cookie: tvmagazine=guia%5Faberto=1; path=/    
Set-Cookie: tvmagazine%2Dvmidtoken=; path=/    
Set-Cookie: tvmagazine%2Dvmid=; path=/    
Set-Cookie: ASPSESSIONIDCSTQTSDC=FBKHFBHCKIEPJECDCCDECKAA; path=/    
Cache-control: private    

<!DOCTYPE html>    
<html lang="pt-br" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">    
<head>

<!-- Meta Tags -->    
<link rel="canonical" href="http://www.tvmagazine.com.br/guia/programacao,NET,0" />    
<meta http-equiv="Cache-Control" content="no-cache, no-store" />    
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />    
<meta http-equiv="Content-Language" content="pt-br" />    
<meta http-equiv="Pragma" content="no-cache" />    
<meta http-equiv="X-UA-Compatible" content="IE=edge" />    
<meta name="author" content="TV Magazine" />    
<meta name="created" content="01/12/2015 07:23:36" />    
<meta property="fb:app_id" content="85553650775" />    
<meta property="fb:page_id" content="146944948751407" />    
<meta name="keywords" content="Guia de Programação, BBB, TV, televisão, cabo, por assinatura, programação, guia, filme, série, seriado, música, variedades, erótico, novela, fórum, notícias, jornalismo, informação, notícia, entretenimento, NET, SKY, TVA, DIRECTV, Telefônica, Vivo, Globo, SBT, Record, SporTV, Big Brother, HBO, Telecine, ESPN, Band, PFC, Premiere, PPV, programação, fórum, youtube, vídeo, vídeos, widget, webslice, pay-per-view" />    
<meta property="og:article:author" content="TV Magazine" />    
<meta property="og:description" content="Guia de Programação da NET - TV Magazine. A TV Magazine é mais completo site sobre televisão do Brasil. Aqui você encontra notícias, um completo guia de programação, fórum de discussões, vídeos e muito mais." />    
<meta property="og:url" content="http://www.tvmagazine.com.br/guia/programacao,NET,0" />     
<meta property="og:image" content="http://www.tvmagazine.com.br/imagens/png/touch_icon.png"/>    
<meta property="og:locale" content="pt_BR" />    
<meta property="og:site_name" content="TV Magazine" />    
<meta property="og:title" content="Guia de Programação da NET - TV Magazine"/>