Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing ArrayList and double array[]

 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I have some code...that doesn't work. I am trying to find the common elements in two arrays. One of them is an array list full of doubles and the second is a double array full of doubles. I am trying to display the value of the element that they have in common and the element number that it is in.

Here is my code:



Thanks in advance.

Gabe.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couple of really obvious mistakes in your "if" condition...



- An double value (array2[i]), which is a primative, can't be casted to a string.

- The "if" condition is expecting a boolean value (or expression) -- not a Double object.

Henry
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Henry, thanks for the reply.

I have since changed my code. I did know about the casting the primitive type, but now I can get it to run, but it is all wrong.

Here is my resulting code looking for an indexOf in the Arraylist.

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve, but still double is required on indicated line in code:

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could turn your array into a List<Double> using Arrays.asList(), and then call the retainAll() method.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that my compiler will accept Generics. It says something like requires 1.5, current version 1.2.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.2 is very old(6 or 7 years I think). Is there a reason you are using such an old compiler?
[ April 03, 2006: Message edited by: Rusty Shackleford ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the compiler knows it requires 1.5 I doubt it's actually that old. Perhaps that's just the default target for their IDE or whatever they're using? @OP: Is there any reason you have to compile code with a 1.2 compliance?
reply
    Bookmark Topic Watch Topic
  • New Topic