【问题标题】:Error Parsing a string to a List将字符串解析为列表时出错
【发布时间】:2012-10-17 05:42:21
【问题描述】:

我已经设法从数据库中获取一个字符串,并能够将它的一些元素存储在变量中,以减少应用程序与数据库交互的次数。但是,我希望从数据库中获取第一个元素并将其存储在列表中,但是当我将字符串解析为新列表时,它会不断产生错误。请帮忙

//String fetched from the database
final String[] rec = split(myresult,seperator);

//loc is the first String to be parsed to a String..
//desc is the 2nd string to be parsed to a textarea
//coords 3rd string which contains coordinates..
String  loc=rec[0];
final  String desc=rec[1];
String coords=rec[2];

//ERROR IS GENERATED HERE!!!
listmboya=new List(loc);
//Separate the coordinates in the string...
String seperator2=",";

String [] coordinates=split(coords,seperator2);

String lat=coordinates[0];
String lot=coordinates[1];

//convert them to floats..
item1=Float.parseFloat(lat);
item2=Float.parseFloat(lot);

【问题讨论】:

    标签: java java-me nokia


    【解决方案1】:

    list 是一个接口, 试试

       listmboya=new ArrayList();
       listmboya.add(loc);
    

    【讨论】:

    • locString。我认为没有ArrayList 构造函数接收String 作为参数。
    • @user1708393 来自发布的代码块,不能保证这将是您问题的最佳解决方案,它只会让您编译到item2=... 行。
    【解决方案2】:

    您不能实例化List 对象。它是interface 而不是class。您需要一个ArrayList,它是List 的实现类,而且ArrayList 没有以String 作为参数的构造函数。

    因此,您需要创建一个ArrayList<String>,然后将您的字符串添加到其中。

    所以你需要这样做:-

    List<String> listmboya=new ArrayList<String>();
    listmboya.add(loc);
    

    确保将您的listmboya 声明为List&lt;String&gt;

    更新: - 发布更多代码以防万一这不起作用。我们需要了解更多。

    【讨论】:

    • 错误消失了。如何将 actionListener 添加到创建的 ArrayList 中?
    • When a particular item on the now ArrayList is selected, there is an event or an action to be Performed (actionPerformed)
    • @user1708393 您可以单击答案旁边的箭头将其标记为已接受。并为您的查询添加一个新问题。 :)
    • 我没有看到箭头。这是我第一次在这里发布问题..抱歉唠叨:)
    【解决方案3】:

    List是抽象类,不能实例化。 试试这样:

    listmboya = new ArrayList<String>();
    listmboya.add(loc);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多