Article View: borland.public.jbuilder.java.language
Article #18851Re: arrays of objects
From: James V. Reagan
Date: Wed, 08 Apr 1998 00:00
Date: Wed, 08 Apr 1998 00:00
59 lines
2437 bytes
2437 bytes
Hi Kevin, In Java everything is a 'reference' (except for primitive types, of course). References are just pointers (in fact, in C++ references are implemented as pointers under the hood). You just can't do pointer arithmetic with references (a la C++). So, all you would need to do is: Object[] array1 = new Object[10000]; Object[] array2 = new Object[100]; Object obj = new Object(); array1[0] = obj; array2[0] = obj; and voila you have your entries from two arrays referencing a single object. In article <01bd62ec$0c857a40$b2c33081@boylankl>, "Kevin B" <boylank@computer.org> wrote: >Hi, > >I'm a C++/Delphi programmer that is fairly new to Java and I'm trying to >makes some sense of something that is easy in either of these languages but >not so intuitive in Java (to me). > >I'd like to be able to create a couple arrays of objects in Java and I'd >like to preallocate the arrays so that I don't have the performance problem >of creating each object separately. This is no problem as I can just do >something like... > >MyObjectType myObjects[] = new MyObjectType[ 10000 ]; > >But what I'd like to do is create another array that will only hold a >subset of the elements from this array. Since the object that makes up >each element of this array is fairly large, I'd like for the elements of >the second array to just be able to POINT at the actual objecs of the first >array. > >This is easy in C++ or Delphi as I can just make the second array be an >array of pointers (of type MyObjectType) and do something like > >myObjects2[i] = &myObjects[j]; > >But since there are no pointers in Java I'm confused as to how to do this >or whether this is even possible. Can somebody straigten me out on this? > >TIA!! > >Kevin > -- James V. Reagan Sun Certified Java Programmer Principal Consultant JPartners, Inc. mailto:jreagan@jpartners.com http://www.jpartners.com Java & OOA/D Consulting, Mentoring, and Development
Technical Details
Message-ID:
<6ggepc$e11$1@Nntp1.mcs.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<01bd62ec$0c857a40$b2c33081@boylankl>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,d90257f1ce51c897 X-Google-Attributes: gidf74b3,public From: jreagan@jpartners.com (James V. Reagan) Subject: Re: arrays of objects Date: 1998/04/08 Message-ID: <6ggepc$e11$1@Nntp1.mcs.net> X-Deja-AN: 342142492 References: <01bd62ec$0c857a40$b2c33081@boylankl> Organization: JPartners, Inc. Newsgroups: borland.public.jbuilder.java.language