我正在使用一个保存商品价格的 Double 变量。该变量存储在 postgresql 数据库中的货币类型列下。我使用 setBigDecimal(position,value) SQL 函数。在其他部分,我使用 JSpinner 作为输入。
Double current = 0.0;
Double min = (double) Integer.MIN_VALUE;
Double max = (double) Integer.MAX_VALUE;
Double step = 0.1;
JSpinner priceSpinner = new JSpinner(new SpinnerNumberModel(current, min, max, step));
Run Code Online (Sandbox Code Playgroud)
当用户单击按钮时,我会获取用户输入的值并通过 SQL 查询将其放入数据库中。
insertStmt.setBigDecimal(position,BigDecimal.valueOf((double) priceSpinner.getValue()));
Run Code Online (Sandbox Code Playgroud)
但是,我得到了这个小错误,
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double
Run Code Online (Sandbox Code Playgroud) 我正在尝试将Any对象的类型转换为Long,但它显示以下异常:
java.lang.ClassCastException:java.lang.Object[] 无法转换为 java.lang.Long
在这里我想将其转换any为Long如下所示:
fun getMyLongValue(vararg any: Any) : Long {
return when(val tmp = any.first()) {
is Number -> tmp.toLong()
else -> throw Exception("not a number")
}
}
Run Code Online (Sandbox Code Playgroud)
我将参数传递给它,例如:
fun main() {
val param1 = 10
val param2 = 20
println(getMyValue(param1.toLong(), param2.toLong()))
}
fun getMyValue(vararg any: Any): Long {
return getMyLongValue(any)
}
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以将s 的Java转换List为Strings数组String:
我试过这个:
List<String> products = new ArrayList<String>();
//some codes..
String[] arrayCategories = (String[]) products.toArray();
Run Code Online (Sandbox Code Playgroud)
但它给了我一个例外信息:
java.lang.ClassCastException:java.lang.Object []无法强制转换为java.lang.String []
我希望得到有序形式的键,所以我使用了Sorted Map,但我得到了"ClassCastException",因为我想知道我的程序中存在的这个问题的原因,或者我做错了什么.请建议我.谢谢!我的示例代码如下:
Run Code Online (Sandbox Code Playgroud)public class TreeTest { public static void main(String[] args) { SortedMap<SectorInfo, List<String>> map2 = new TreeMap<TreeTest.SectorInfo, List<String>>(); ArrayList<String> list = new ArrayList<String>(); ArrayList<String> list1 = new ArrayList<String>(); ArrayList<String> list2 = new ArrayList<String>(); list.add("Test1"); list.add("Test2"); list1.add("Test3"); list1.add("Test4"); list2.add("Test5"); list2.add("Test6"); map2.put(new SectorInfo("S1", "P1"), list); map2.put(new SectorInfo("S2", "P2"), list1); map2.put(new SectorInfo("S3", "P3"), list2); for (SectorInfo sectorInfo : map2.keySet()) { System.out.println(SectorInfo.pName +" In " + SectorInfo.sName); } } protected static class SectorInfo { public String sName; public String pName; SectorInfo(String sName, String …
我在下面写了代码.它得到这样的信息:
线程"main"中的异常java.lang.ClassCastException:[D无法强制转换为java.lang.Double
double[] xyz = {1, 11, 111, 2, 22, 222};
ArrayList array = new ArrayList();
array.add(xyz);
double[] vals = new double[array.size()];
vals[0] = (double) array.get(0);
vals[1] = (double) array.get(1);
vals[2] = (double) array.get(2);
Run Code Online (Sandbox Code Playgroud)
我也搜索并看到Stack Overflow上的一些帖子,但它们并没有让我感觉到多少.我该怎么办?
HashMap h = new HashMap();
Collection c = h.values();
Object[] a = c.toArray();
LinkedList<Object[]> l = new LinkedList<Object[]>();
l.addFirst(a);
TreeSet<Object[]> t = new TreeSet<Object[]>(l); //throws ClassCastException exception
Run Code Online (Sandbox Code Playgroud)
由于我没有违反任何合同,这种例外很奇怪.
我正在开发Android应用程序,并且已经实现了使用共享首选项进行所有处理的类。此类拥有用于获取首选项的通用方法,该方法具有通用性,它根据参数中的类型返回不同类型的数据类型。我存储到共享首选项和从共享首选项检索的数据类型为整数,字符串,布尔值和列表(使用JSON将列表序列化/反序列化到字符串或从字符串反序列化)。好了,一切都很好,我可以通过这种方式从共享的偏好中成功获取int,string和boolean,但不能成功获取List,因此我得到了:
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
Run Code Online (Sandbox Code Playgroud)
我忘了告诉我们该方法已声明要返回一个Object,并且在调用该方法时,将返回值类型转换为正确的类型。无论如何,我不明白为什么它说不能转换字符串,甚至不返回字符串?这是执行此操作的正确方法吗,我的意思是返回对象然后进行转换完全正确。
以下用于获取共享首选项的完整方法。感谢您的任何建议!
public Object getPrefs(String sharedPreferences, String key, int type,
Context context, Object defaultObject) {
// A String indicating an error occurred while retrieving shared
// preferences
final String ERROR = "ERROR";
// Set shared preferences from context
sharedPref = context.getSharedPreferences(sharedPreferences,
Context.MODE_PRIVATE);
switch (type) {
case (0): // <-- Integer
// Check that defaultObject is of correct instance else collect
// "hardcoded" default value of 0
if (defaultObject instanceof Integer) {
this.logger.logCatTxt( …Run Code Online (Sandbox Code Playgroud)