# convert double to int in ArryList

Printable View

• 08-05-2013, 02:16 PM
vector_ever
convert double to int in ArryList
Hello,

assume that i have the flowing arrayList:

Code:

```ArrayList arr = new arrayList(); arr.add("hi"); arr.add(2.5); arr.add("x"); arr.add(2.9); arr.add(1.0);```
now i want to convert every double value in arr to integer, how to do that (just the double and ignore another types)
• 08-05-2013, 02:25 PM
KevinWorkman
Re: convert double to int in ArryList
What have you tried?
• 08-05-2013, 02:26 PM
KevinWorkman
Re: convert double to int in ArryList
• 08-05-2013, 02:52 PM
Tolls
Re: convert double to int in ArryList
Well, one way would be to not make the mistake of populating your list with seemingly random datatypes.
• 08-05-2013, 03:16 PM
vector_ever
Re: convert double to int in ArryList
Code:

```        public static void main(String[] args) {                 ArrayList arr = new ArrayList();                 arr.add("hi");                 arr.add(2.5);                 arr.add("x");                 arr.add(2.9);                 arr.add(1.0);                 for(int i = 0; i < arr.size(); i++){                 if (arr.get(i) instanceof Double ){                         (int)arr.get(i); //false, but how to do it correctly                 }         }         } }```
• 08-05-2013, 03:21 PM
gimbal2
Re: convert double to int in ArryList
Have you considered checking out the API documentation of the Double class? Perhaps there is a method in there that does what you want.

Google: java 7 Double
Result: Double (Java Platform SE 7 )
• 08-05-2013, 03:52 PM
JosAH
Re: convert double to int in ArryList
Why did you put those Doubles in that (raw) List in the first place? Couldn't you have checked and casted them before you put them in that List?

kind regards,

Jos
• 08-05-2013, 04:03 PM
vector_ever
Re: convert double to int in ArryList
Quote:

Google: java 7 Double
Result: Double (Java Platform SE 7 )
Thank Erik, i read it but not new in it, still have no soultion

Quote:

Why did you put those Doubles in that (raw) List in the first place? Couldn't you have checked and casted them before you put them in that List?
hi jos, i just looking a solution to this case, jut when they are already in the arraylist
• 08-05-2013, 04:13 PM
Tolls
Re: convert double to int in ArryList
But that's your problem.
Not how to identify them and convert them, but to not have them there in the first place.
You shouldn't be in this position.
• 08-05-2013, 04:29 PM
vector_ever
Re: convert double to int in ArryList
your solution is proper just if i have another choice, i have library an it read some parameter from the internal network and store them in ArrayList, and i have no access to these Parameters before add them to arraylist
• 08-05-2013, 04:56 PM
KevinWorkman
Re: convert double to int in ArryList
Again, the API is your best friend: ArrayList (Java Platform SE 7 )

You could also build a new ArrayList, but as others have said, this whole thing has a pretty bad code smell.
• 08-05-2013, 04:59 PM
gimbal2
Re: convert double to int in ArryList
Quote:

Originally Posted by vector_ever
your solution is proper just if i have another choice, i have library an it read some parameter from the internal network and store them in ArrayList, and i have no access to these Parameters before add them to arraylist

Now hopefully you know why hiding information only makes it more difficult for people to help you. If you would have told from the very beginning where that ArrayList is coming from, then I'm guessing that Tolls and JosAH wouldn't have wasted their time to help you correct your requirements in stead of the code. As you present it now, we could have only assumed that you are creating this ArrayList yourself.

Back to the matter at hand. Your current problem state:

- you have an Object (arr.get(i) returns an Object); you need a Double. You must know about type casting since you have that (int) in your code
- apparently when you have your Double, you actually want to convert it to an int, or else I would understand what that (int) typecast is for

The first you should be able to solve yourself, the second really can be solved by taking another look at the Double API. There is a method in there that can help you. I'm not going to spell it out, it is high time that you learn how to find (trivial) information for yourself.
• 08-05-2013, 08:05 PM
vector_ever
Re: convert double to int in ArryList
I don't know why you think that i hide the information or try to waste every body's time !!

Any way to solve my self as you mean i talk second and third look at Double API, i don't find anything i can use it in my question unless if you mean .intValue(), because i test it with my code and nothing changed
Code:

```        for(int i = 0; i < arr.size(); i++){         if (arr.get(i) instanceof Double ){             ((Double)arr.get(i)).intValue();         }```
The double don't get changed to int.
• 08-05-2013, 08:16 PM
gimbal2
Re: convert double to int in ArryList
Quote:

Originally Posted by vector_ever
I don't know why you think that i hide the information or try to waste every body's time !!

Try to understand what I'm saying in stead of just assuming the worst.

Quote:

i can use it in my question unless if you mean .intValue()
bingo.

Quote:

The double don't get changed to int.
Well then you did it wrong. Take a little longer to figure it out. Start by cleaning up the code so it is more readable.
• 08-05-2013, 08:31 PM
KevinWorkman
Re: convert double to int in ArryList
I provided the link to the API for ArrayList. That contains methods you need to use.

Casting a value does not automatically update any references to that value. For example, if I have something like:

Code:

```double d = 1.3; (int)1.3; System.out.println(d);```
It's not going to magically make d an int, or even put 1.0 in d. You have to update any references to the value you want to change, including those inside the ArrayList. Hence the link to the API for useful functions.
• 08-05-2013, 09:08 PM
vector_ever
Re: convert double to int in ArryList
Code:

```        public static void main(String[] args) {         ArrayList arr = new ArrayList();         arr.add("hi");         arr.add(2.5);         arr.add("x");         arr.add(2.9);         arr.add(1.0);           for(int i = 0; i < arr.size(); i++){         if (arr.get(i) instanceof Double ){                 int n = ((Double) arr.get(i)).intValue();                 arr.set(i, n);         }         System.out.println(arr.get(i));     }     }```
Thanks