我无法初始化List,如下面的代码所示:
List<String> supplierNames = new List<String>();
supplierNames.add("sup1");
supplierNames.add("sup2");
supplierNames.add("sup3");
System.out.println(supplierNames.get(1));
Run Code Online (Sandbox Code Playgroud)
我面临以下错误:
无法实例化该类型
List<String>
我该如何实例化List<String>?
当我尝试运行此代码时:
import java.io.*;
import java.util.*;
public class TwoColor
{
public static void main(String[] args)
{
Queue<Edge> theQueue = new Queue<Edge>();
}
public class Edge
{
//u and v are the vertices that make up this edge.
private int u;
private int v;
//Constructor method
public Edge(int newu, int newv)
{
u = newu;
v = newv;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot instantiate the type Queue
at TwoColor.main(TwoColor.java:8)
我不明白为什么我不能实例化这个课......对我来说似乎是对的......
我正在尝试创建一个字符串集,其中填充了来自Hashtable的键,因此for-each循环可以遍历Set并将默认值放在Hashtable中.我仍然在学习Java,但我尝试这样做的方式并不是有效的语法.有人可以证明这样做的正确方法,并解释为什么我的方式不起作用,他们的方式.
private Hashtable<String, String> defaultConfig() {
Hashtable<String, String> tbl = new Hashtable<String, String>();
tbl.put("nginx-servers","/etc/nginx/servers");
tbl.put("fpm-servers","/etc/fpm/");
tbl.put("fpm-portavail","9001");
tbl.put("webalizer-script","/usr/local/bin/webalizer.sh");
tbl.put("sys-useradd","/sbin/useradd");
tbl.put("sys-nginx","/usr/sbin/nginx");
tbl.put("sys-fpmrc","/etc/rc.d/php_fpm");
tbl.put("www-sites","/var/www/sites/");
tbl.put("www-group","www");
return tbl;
}
//This sets missing configuration options to their defaults.
private void fixMissing(Hashtable<String, String> tbl) {
Hashtable<String, String> defaults = new Hashtable<String, String>(defaultConfig());
//The part in error is below...
Set<String> keys = new Set<String>(defaults.keySet());
for (String k : keys) {
if (!tbl.containsKey(k)) {
tbl.put(k, defaults.get(k));
}
}
}
Run Code Online (Sandbox Code Playgroud) 我已将.jars添加到我的库中,我还可以在JAR中实例化其他类.是什么赋予了?我试图清理项目等.
这是编译时错误.Eclipse不会让我实例化它.
我正在导入正确的库,而不是sun版本,并使用他们的教程指定的默认构造函数
HttpClient client = new HttpClient();
Run Code Online (Sandbox Code Playgroud)
(Eclipse,mac,Apache HTTP,"HttpClient 4.0.1(GA)"从这里下载)
我尝试在导航抽屉中创建一个ExpandableListView.我尝试了一个示例代码但是在编译之前我遇到了这个错误.
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.left_drawer);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
Run Code Online (Sandbox Code Playgroud)
错误在这一行:listAdapter = new ExpandableListAdapter(this,listDataHeader,listDataChild);
为什么它说无法实例化ExpandableListAdapter类型?
如何在java中创建对象类型的空列表?
我正在尝试创建以下对象的列表。
List<Categories> catList= new List<Categories>();
Run Code Online (Sandbox Code Playgroud)
我已经导入了模块java.util.List,但是我收到错误“无法实例化类型列表”
顺便说一句,我尝试过这个,但收到此错误“局部变量尚未初始化”
List<Categories> catList;
catList.add(categoryItem);
Run Code Online (Sandbox Code Playgroud)