开发者

Data array values incompatible types

Not sure why this is wrong I am new to java so please help if you can

ncompatible types

found : java.lang.Object

required: java.l开发者_运维百科ang.String

String    sDividendDate1     = data[0][0];

Where

Object[][] data = {
    { "Tom Jones", new Double(5) }, 
    { "Paul Smith", new Double(5) }, 
    { "yyyy/mm/dd", new Double(5) }
}

Is data[0][0] not the string "Tom Jones" ?

How do I get data that I need from my data array?

Any help will be greatly appreciated - Many thanks Stephen


I'm guessing that this is a compiler error, because the compiler realizes it cannot assume that all objects are strings. Try casting your returned value like so:

String sDividendDate1 = (String)data[0][0];

This tells the compiler that you recognize the array might not return a String, but that you're willing to risk it because you think you know what that value will actually be at runtime. If the code finds that there isn't really a string there when it goes to do the cast, you'll get a runtime exception.

Edit

Andy K has a good point about type safety. Based on the example you gave it seems like you're trying to represent data that is interrelated. If you can think of names to represent the way the data is related, you can model this more naturally using a class, like so:

public class PlayerAverage {
    public PlayerAverage(String name, double average) {
        this.name = name;
        this.average = average
    }
    private String name;
    private double average;

    public String getName() {return name;}
    public double getAverage() {return average;}
}

Then you could create an array of these:

PlayerAverage[] playerAverages = {
    new PlayerAverage("Tom Jones", 5),
    new PlayerAverage("Paul Smith", 5)
};

Because it would be typed, you could easily iterate over this array without the need for any type casting:

for(PlayerAverage playerAverage : playerAverages) {
    String name = playerAverage.getName();
    double average = playerAverage.getDouble();
}

Note that this approach uses a lot of best-practices like private fields with public getters, which might be overkill for what you're doing. But it's generally a good idea to create classes to indicate how your data is related and what it is supposed to represent.


Although you may cast to fix this, it would be better to improve your code to get type safety. If possible, use a map.

import java.util.*;
Map<String, Double> map = new HashMap<String, Double>();
map.put("Tom Jones", 5.0);
map.put("Paul Smith", 5.0);
map.put("yyyy/mm/dd", 5.0);


String sDividendDate1 = data[0][0].toString(); //return string

Object class is superclass for String class, so sDividendDate1 cannot contain variable of Object class


Your description is missing some closing braces. Right now, it's an array of array of array of array...that's never closed. I'd expect to use:

Object[][] data = { {"Tom Jones", new Double(5)}, {"Paul Smith", new Double(5)}, {"yyyy/mm/dd", new Double(5) }};

Edit: Kirk edited it and fixed the missing braces, but I'm thinking those missing braces were causing the error message you saw at compilation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜