(JAVA) 如何将 double 从列表转换为 int
(JAVA) How to convert a double from a list to an int
我制作了一个名为 list
的列表和两个名为 Weight
和 Age
的双打
然后我尝试像这样从列表中获取一个元素 list.get((Age * 2) - 1)
,但它告诉我不能在 get()
函数中放入双精度数,所以我尝试这样做 (int)(Age-1)
但是当我像这样把那段代码放在 if()
命令中时
if(list.get((int) ((Age * 2) - 1)) > Weight.intValue())
但出于某种原因,它给了我这个例外:
operator ">" cannot be applied to "java.lang.Object","int"
为什么,就是为什么!
总的来说,这是我的代码(请注意,我用一些额外的 android 库编写了它)
package com.example.ademapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.Start);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.Weight);
EditText text3 = (EditText)findViewById(R.id.Age);
Double Weight = Double.parseDouble(text.getText().toString());
EditText text2 = (EditText)findViewById(R.id.Height);
Double Height = Double.parseDouble(text2.getText().toString());
Double IMC = Weight/Height/Math.pow(Height/100,2);
TextView Result=(TextView)findViewById(R.id.Result);
List list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);
Double Age = Double.parseDouble(text3.getText().toString());
RadioButton Adult = (RadioButton) findViewById(R.id.Adult);
RadioButton Kid = (RadioButton) findViewById(R.id.Kid);
if(Adult.isChecked())
{
if (IMC<18.5)
{
Result.setText("Tu est Trop Maigre!");
}
else if (IMC>30)
{
Result.setText("Tu est Trop Gras!");
}
else
{
Result.setText("Bravo! Tu est en bonne santée!");
}
}
else if (Kid.isChecked())
{
if(list.get((int) ((Age * 2) - 1)) > Weight.intValue())
}
}
});
}
}
您使用的不是通用列表。
List list = Arrays.asList(15,19, ... , 25)
如果列表不是通用的,它包含对象。(java中最常见的class)
您将 int 和 double 存储在一起,因此您需要由它们的父项
泛化它
List<? extends Number> list = Arrays.asList(15,19, ... , 25)
然后取它的double值,虽然不能把double转成int,但是把int转成double是可以的
if(list.get((int) ((Age * 2) - 1)).doubleValue() > Weight.intValue())
tl;博士
使用 <Double>
声明列表内容的类型。
List < Double > inputDoubles = List.of( 15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25 ) ;
使用泛型
正如 khelwood 评论的那样,请参阅:What is a raw type and why shouldn't we use it?。
使用 generics feature of Java,您可以在其中指定 List
集合要保存的数据类型。如果不指定要保存的类型,< Double >
,列表不知道检索到的元素的类型。
如果没有泛型,当您调用 list.get( … )
时,编译器只会看到一个 Object
object rather than a Double
对象。您的代码 > Weight.intValue()
要求将 Object
对象与 int
基元进行比较,这没有任何意义,因为 Object
对象不是数字。
而不是这个:
List list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);
…将 < Double >
添加到您的 List
声明中,如下所示:
List < Double > list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);
您刚刚告诉编译器需要这个特定的 List
object is a list of Double
objects. Code that puts a DayOfWeek
, Dog
, or Invoice
object into that list will not compile. Conversely, when retrieving an element from the list, the compiler knows for sure the retrieved element is the correct expected type, a Double
. So no casting。
而且,顺便说一句,我们现在有了使用 List.of
. The result is a non-modifiable list 的更方便的文字语法。
通常最好在变量上使用更具描述性的名称,例如 inputDoubles
而不是 list
。
List < Double > inputDoubles = List.of( 15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25 ) ;
我制作了一个名为 list
的列表和两个名为 Weight
和 Age
然后我尝试像这样从列表中获取一个元素 list.get((Age * 2) - 1)
,但它告诉我不能在 get()
函数中放入双精度数,所以我尝试这样做 (int)(Age-1)
但是当我像这样把那段代码放在 if()
命令中时
if(list.get((int) ((Age * 2) - 1)) > Weight.intValue())
但出于某种原因,它给了我这个例外:
operator ">" cannot be applied to "java.lang.Object","int"
为什么,就是为什么!
总的来说,这是我的代码(请注意,我用一些额外的 android 库编写了它)
package com.example.ademapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.Start);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.Weight);
EditText text3 = (EditText)findViewById(R.id.Age);
Double Weight = Double.parseDouble(text.getText().toString());
EditText text2 = (EditText)findViewById(R.id.Height);
Double Height = Double.parseDouble(text2.getText().toString());
Double IMC = Weight/Height/Math.pow(Height/100,2);
TextView Result=(TextView)findViewById(R.id.Result);
List list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);
Double Age = Double.parseDouble(text3.getText().toString());
RadioButton Adult = (RadioButton) findViewById(R.id.Adult);
RadioButton Kid = (RadioButton) findViewById(R.id.Kid);
if(Adult.isChecked())
{
if (IMC<18.5)
{
Result.setText("Tu est Trop Maigre!");
}
else if (IMC>30)
{
Result.setText("Tu est Trop Gras!");
}
else
{
Result.setText("Bravo! Tu est en bonne santée!");
}
}
else if (Kid.isChecked())
{
if(list.get((int) ((Age * 2) - 1)) > Weight.intValue())
}
}
});
}
}
您使用的不是通用列表。
List list = Arrays.asList(15,19, ... , 25)
如果列表不是通用的,它包含对象。(java中最常见的class) 您将 int 和 double 存储在一起,因此您需要由它们的父项
泛化它List<? extends Number> list = Arrays.asList(15,19, ... , 25)
然后取它的double值,虽然不能把double转成int,但是把int转成double是可以的
if(list.get((int) ((Age * 2) - 1)).doubleValue() > Weight.intValue())
tl;博士
使用 <Double>
声明列表内容的类型。
List < Double > inputDoubles = List.of( 15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25 ) ;
使用泛型
正如 khelwood 评论的那样,请参阅:What is a raw type and why shouldn't we use it?。
使用 generics feature of Java,您可以在其中指定 List
集合要保存的数据类型。如果不指定要保存的类型,< Double >
,列表不知道检索到的元素的类型。
如果没有泛型,当您调用 list.get( … )
时,编译器只会看到一个 Object
object rather than a Double
对象。您的代码 > Weight.intValue()
要求将 Object
对象与 int
基元进行比较,这没有任何意义,因为 Object
对象不是数字。
而不是这个:
List list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);
…将 < Double >
添加到您的 List
声明中,如下所示:
List < Double > list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);
您刚刚告诉编译器需要这个特定的 List
object is a list of Double
objects. Code that puts a DayOfWeek
, Dog
, or Invoice
object into that list will not compile. Conversely, when retrieving an element from the list, the compiler knows for sure the retrieved element is the correct expected type, a Double
. So no casting。
而且,顺便说一句,我们现在有了使用 List.of
. The result is a non-modifiable list 的更方便的文字语法。
通常最好在变量上使用更具描述性的名称,例如 inputDoubles
而不是 list
。
List < Double > inputDoubles = List.of( 15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25 ) ;