Results 1 to 16 of 16
- 08-05-2013, 03:16 PM #1
Senior Member
- Join Date
- Jul 2013
- Posts
- 188
- Rep Power
- 8
convert double to int in ArryList
Hello,
assume that i have the flowing arrayList:
Java Code:ArrayList arr = new arrayList(); arr.add("hi"); arr.add(2.5); arr.add("x"); arr.add(2.9); arr.add(1.0);
- 08-05-2013, 03:25 PM #2
Re: convert double to int in ArryList
What have you tried?
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-05-2013, 03:26 PM #3
Re: convert double to int in ArryList
Crossposted: convert double to int in ArryList
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-05-2013, 03:52 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.
Please do not ask for code as refusal often offends.
** This space for rent **
- 08-05-2013, 04:16 PM #5
Senior Member
- Join Date
- Jul 2013
- Posts
- 188
- Rep Power
- 8
Re: convert double to int in ArryList
Java 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, 04:21 PM #6
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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 )"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-05-2013, 04:52 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-05-2013, 05:03 PM #8
Senior Member
- Join Date
- Jul 2013
- Posts
- 188
- Rep Power
- 8
Re: convert double to int in ArryList
Google: java 7 Double
Result: Double (Java Platform SE 7 )
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?Last edited by vector_ever; 08-05-2013 at 05:06 PM.
- 08-05-2013, 05:13 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.Please do not ask for code as refusal often offends.
** This space for rent **
- 08-05-2013, 05:29 PM #10
Senior Member
- Join Date
- Jul 2013
- Posts
- 188
- Rep Power
- 8
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, 05:56 PM #11
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.How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-05-2013, 05:59 PM #12
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: convert double to int in ArryList
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."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-05-2013, 09:05 PM #13
Senior Member
- Join Date
- Jul 2013
- Posts
- 188
- Rep Power
- 8
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
Java Code:for(int i = 0; i < arr.size(); i++){ if (arr.get(i) instanceof Double ){ ((Double)arr.get(i)).intValue(); }
- 08-05-2013, 09:16 PM #14
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: convert double to int in ArryList
Try to understand what I'm saying in stead of just assuming the worst.
i can use it in my question unless if you mean .intValue()
The double don't get changed to int."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-05-2013, 09:31 PM #15
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:
Java Code:double d = 1.3; (int)1.3; System.out.println(d);
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-05-2013, 10:08 PM #16
Senior Member
- Join Date
- Jul 2013
- Posts
- 188
- Rep Power
- 8
Re: convert double to int in ArryList
Java 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)); } }
Similar Threads
-
Convert double to Date
By javaWannabe in forum New To JavaReplies: 3Last Post: 03-29-2012, 11:51 PM -
Convert from string to double
By Lord ice in forum New To JavaReplies: 4Last Post: 12-12-2010, 06:27 PM -
How to convert a double into a int?
By tyang in forum New To JavaReplies: 4Last Post: 02-10-2010, 11:02 AM -
convert String to Double
By azurovyhrosik in forum CLDC and MIDPReplies: 5Last Post: 10-22-2008, 03:46 AM -
convert string to a double?
By javaMike in forum Advanced JavaReplies: 2Last Post: 11-27-2007, 04:10 AM
Bookmarks