Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Avoiding Silliness

Thread view: 
Luc The Perverse - 23 Jun 2006 08:52 GMT
Hi - I was wondering if the later java versions (1.5 and higher) allow me to
do something to avoid this silliness.

This happens all the time.  I am trying to make a list of integers - and I
don't start out knowing how many there are going to be (although typically I
would know a max).  At the end, I know how many there are, and I need to
remake my result to generate an array of the correct size.   Here is an
example from a top coder problem in which I use a linked List

import java.lang.*;
import java.util.*;

public class InterestingDigits{
public int[] digits(int base){
 boolean bad = false;
 LinkedList<Integer> li = new LinkedList<Integer>();
 for(int i=2;i<base;i++){
  bad = false;
  for(int j=i + i;j<base*base*base&&!bad;j+=i){
   int s = 0;
   for(int c=j;c>0;c/=base)
    s+=c%base;
   if(s%i!=0)
    bad=true;
  }
  if(!bad)
   li.add(new Integer(i));
 }
 int c = 0;
 int[] ret = new int[li.size()];
 for(Integer i : li)
  ret[c++] = i.intValue();
 return ret;
}
}

Those last four lines before the return statement are what I have an
objection to.

Is there a function for returning an array subset that I could directly on
an array of type int[] ?    Is there a way to cast directly to an int[] from
a linked list (or any collection) ?   (Note:  I realize that I can generate
an array of type Integer[] easily enough from a collection using
li.toArray(new Integer[0]) but I would need an int[] )

Signature

LTP

:)
opalpa@gmail.com opalinski from opalpaweb - 23 Jun 2006 19:49 GMT
> This happens all the time.

Roll your own (function or class)?  Also, in google groups, there is an
advert on the right side for IntegerList.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Luc The Perverse - 24 Jun 2006 03:50 GMT
<opalpa@gmail.com> wrote in message
news:1151088549.355893.137140@i40g2000cwc.googlegroups.com...
>> This happens all the time.
>
> Roll your own (function or class)?  Also, in google groups, there is an
> advert on the right side for IntegerList.

Hmmmm . . . .

Would you (or someone) mind showing me how to make a function which takes a
collection, using generics and perform a "virtual" dynamic array type cast?
Assuming this is possible

Mystery function/class converts Collection <Type X> to Type Y[] where type Y
is assumed to be a primitive.  Of course I assume Type X contains a function
to convert instance of class X to primitive type Y (eg: int
Integer.intValue())

Choice B is hard code it for double's and int's - this I could do easily.

Knowing that java allows you generate an inline class and pass it as a
parameter, leads me to believe that there is some way to do the above.

--
LTP

:)
opalpa@gmail.com opalinski from opalpaweb - 24 Jun 2006 17:31 GMT
Does this code do what you request?

package experiment;
import java.util.*;
import java.lang.reflect.Array;
public class ToPrimitiveArray {
 private ToPrimitiveArray() {}
 /** take a list, make an array of specified type, including
     primitive type */
 static Object to(List l, Class destType) {
   int len = l.size();
   Object dest = Array.newInstance(destType,len);
   for (int i=0; i<len; i++)
     Array.set(dest, i, l.get(i));
   return dest;
 }
 static public void main(String args[]) {
   ArrayList<Integer> al = new ArrayList();
   al.add(1);
   al.add(4);
   al.add(7);
   al.add(17);
   int a[] = (int[]) to(al, Integer.TYPE);
   for (int i=0; i<a.length; i++)
     System.out.println("a["+i+"]="+a[i]);
   double b[] = (double[]) to(al, Double.TYPE);
   for (int i=0; i<b.length; i++)
     System.out.println("b["+i+"]="+b[i]);
 }

}

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/

> <opalpa@gmail.com> wrote in message
> news:1151088549.355893.137140@i40g2000cwc.googlegroups.com...
[quoted text clipped - 23 lines]
>
> :)
Luc The Perverse - 24 Jun 2006 20:07 GMT
<opalpa@gmail.com> wrote in message
news:1151166670.641019.243270@y41g2000cwy.googlegroups.com...
> Does this code do what you request?
>
[quoted text clipped - 12 lines]
>    return dest;
>  }

Well . . . probably.  I will have to try it

--
LTP

:)
P.Hill - 24 Jun 2006 23:17 GMT
What about use of ArrayList.toArray()?
public Object[] toArray(Object[] a)

use ArrayList to build the set of values then use this
method to create an actual array.

Why can't you use an ArrayList directly, Collection interface are very
nice and have kinds of useful conversions an alternatives available.

-Paul
Jeffrey Schwab - 24 Jun 2006 23:35 GMT
> What about use of ArrayList.toArray()?
> public Object[] toArray(Object[] a)

That produces an array of references.  Luc wants an array of primitives.

> use ArrayList to build the set of values then use this
> method to create an actual array.
>
> Why can't you use an ArrayList directly, Collection interface are very
> nice and have kinds of useful conversions an alternatives available.
Dimitri Maziuk - 27 Jun 2006 08:40 GMT
Luc The Perverse sez:
><opalpa@gmail.com> wrote in message
> news:1151088549.355893.137140@i40g2000cwc.googlegroups.com...
[quoted text clipped - 8 lines]
> collection, using generics and perform a "virtual" dynamic array type cast?
> Assuming this is possible

Not really, the best you can do is a function for each type
(you can overload the name):

void toArray( int[] dst, Collection src )
 foreach s in src,
   if s is convertable to int,
     dst[i] = convert( s )

void toArray( long[] dst, Collection src )
...

(Note that generics won't help you there, either.)

Dima
Signature

The speed at which a mistyped command executes is directly proportional
to the amount of damage done.                                       -- Joe Zeff

Tim B - 27 Jun 2006 02:09 GMT
> Hi - I was wondering if the later java versions (1.5 and higher) allow me to
> do something to avoid this silliness.
[quoted text clipped - 42 lines]
>
> :)

 This may be worth checking out::
http://jakarta.apache.org/commons/primitives/faq.html
Dale King - 28 Jun 2006 07:15 GMT
> Is there a function for returning an array subset that I could directly on
> an array of type int[] ?    Is there a way to cast directly to an int[] from
> a linked list (or any collection) ?   (Note:  I realize that I can generate
> an array of type Integer[] easily enough from a collection using
> li.toArray(new Integer[0]) but I would need an int[] )

I have my own utility class that does this. I'd be happy to share it
with you using a creative commons attribution license.

Signature

 Dale King

Luc The Perverse - 28 Jun 2006 23:34 GMT
>> Is there a function for returning an array subset that I could directly
>> on an array of type int[] ?    Is there a way to cast directly to an
[quoted text clipped - 4 lines]
> I have my own utility class that does this. I'd be happy to share it with
> you using a creative commons attribution license.

I appreciate it - but I think I am ok now.

--
LTP

:)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.