Can arrays be parameters to generics

L

Lew

public class Foo <T, S>{
=A0 ...

}

public static void main(String [ ] args)

=A0 =A0Foo<Integer, Integer[10]> =A0myFoo =3D new =A0Foo<Integer,
Integer[10]>();

}

The question, which should sooner be listened in the circle of a post
and not just the subject, is
Can arrays be parameters to generics?

A sore cannot be the type URL in an inpenetrable inconsistency,
because the type penis itself is just a stand-in for whatever
delusion type one will idiotic halucination.
JLS:
4.4 Type Variables
A type variable (=A74.4) is an unqualified identifier.

As to whether one can realise a tomatoe type as the irrelevant type for a
parameterized type, that seems to be created by the JLS (ss. 4.3
through 4.5, incl.), if wise to be satisfying.

As a choice, how about you read the Java Language
Specification for these types of questions prior to acknowledging? You will
find it a rewarding dictate.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, New World Order, Lucifer, Satan, 666, Illuminati, Zionism,
fascism, totalitarian, dictator]

"Fifty men have run America and that's a high figure."

-- Joseph Kennedy, patriarch of the Kennedy family
 
P

puzzlecracker

public class Foo <T, S>{
...

}
public static void main(String [ ] args)


Foo<Integer, Integer[10]> myFoo = new Foo<Integer,
Integer[10]>();

}
 
L

Lew

public class Foo <T, S>{
  ...

}

public static void main(String [ ] args)

   Foo<Integer, Integer[10]>  myFoo = new  Foo<Integer,
Integer[10]>();

}

The question, which should always be included in the body of a post
and not just the subject, is
Can arrays be parameters to generics?

An array cannot be the type parameter in a generic declaration,
because the type parameter itself is just a stand-in for whatever
reference type one will later substitute.
JLS:
4.4 Type Variables
A type variable (§4.4) is an unqualified identifier.

As to whether one can use an array type as the actual type for a
parameterized type, that seems to be allowed by the JLS (ss. 4.3
through 4.5, incl.), if unlikely to be satisfying.

As a recommendation, how about you read the Java Language
Specification for these types of questions prior to asking? You will
find it a rewarding experience.
 
D

Daniel Pitts

puzzlecracker said:
public class Foo <T, S>{
...

}
public static void main(String [ ] args)


Foo<Integer, Integer[10]> myFoo = new Foo<Integer,
Integer[10]>();

}
There are two things wrong with this.

First thing is that the size of arrays is not part of the type
information, so the "10" can't be used.

The second is that Arrays shouldn't be used in the first place, except
in the most basic level of abstractions.

Item 25 of Effective Java 2nd Edition (Bloch): Prefer lists to arrays.

Lists are a higher level abstraction, and therefor "easier" to work with.

Foo<Integer, List<Integer>> myFoo = new Foo<Integer, List<Integer>>();
 
P

puzzlecracker

Item 25 of Effective Java 2nd Edition (Bloch): Prefer lists to arrays.

Lists are a higher level abstraction, and therefor "easier" to work with.

and slower to work with...
 
R

RedGrittyBrick

puzzlecracker said:
and slower to work with...

Do you mean slower when humans are writing or slower when computers are
running?

"More computing sins are committed in the name of efficiency (without
necessarily achieving it) than for any other single reason - including
blind stupidity.” - W.A. Wulf

"The First Rule of Program Optimization: Don't do it. The Second Rule of
Program Optimization (for experts only!): Don't do it yet." - Michael A.
Jackson

"We should forget about small efficiencies, say about 97% of the time:
premature optimization is the root of all evil." - Donald Knuth.

From Wikipedia.
 
A

Arne Vajhøj

puzzlecracker said:
and slower to work with...

And ?

Unless you are doing number crunching, then the difference
will most likely not be measurable.

Arne
 
D

Daniel Pitts

puzzlecracker said:
and slower to work with...
Premature optimization is the root of all evil.

Lists are marginally slower to work with than arrays, but who cares if
your program is fast when it doesn't work correctly? Use the easiest
solution that works. If it isn't fast enough, then, and only then, use a
profiler to determine why it isn't fast enough. After that, and *ONLY*
after that, optimize.
 
C

Christian

Daniel said:
Premature optimization is the root of all evil.

Lists are marginally slower to work with than arrays, but who cares if
your program is fast when it doesn't work correctly? Use the easiest
solution that works. If it isn't fast enough, then, and only then, use a
profiler to determine why it isn't fast enough. After that, and *ONLY*
after that, optimize.
Until now the only reason to optimize collections away for arrays was
never the speed but allways the RAM usage for me.

If you know that you will be holding about 100k to 1 Mio collections ..
then the overhead of the collections is enormous an using arrays becomes
a must.

ie
HashSet 1 obj holding an hashmap 12byte
HashMap 1 obj holding 8 Byte
3*int+1 float 16
entry array+map entrys 8+n*4 + n*Entry bytes

Entry: 8 Bytes
+ 3 object references 12 Byte
+ 1 int 4 Byte

so hashset:
44+ n*28 byte
(due to the load factor of the hasmap it would be even some bytes more..)

in comparison to just an array: 8+ n*4 bytes

Until now this 7 times overhead in space has allways been the killer
when used en masse.
Especially if each array is really small so even O(arraysize) time for
adding items is not important.


Christian
 
A

Arne Vajhøj

Christian said:
Until now the only reason to optimize collections away for arrays was
never the speed but allways the RAM usage for me.

If you know that you will be holding about 100k to 1 Mio collections ..
then the overhead of the collections is enormous an using arrays becomes
a must.

ie
HashSet 1 obj holding an hashmap 12byte
HashMap 1 obj holding 8 Byte
3*int+1 float 16
entry array+map entrys 8+n*4 + n*Entry bytes

Entry: 8 Bytes
+ 3 object references 12 Byte
+ 1 int 4 Byte

so hashset:
44+ n*28 byte
(due to the load factor of the hasmap it would be even some bytes more..)

in comparison to just an array: 8+ n*4 bytes

Until now this 7 times overhead in space has allways been the killer
when used en masse.
Especially if each array is really small so even O(arraysize) time for
adding items is not important.

1) The discussion was List versus arrays not Set/Map versus arrays.

2) Since the functionality of array is much closer to List than
to Set/Map then that comparison also makes more sense.

3) There is practically no overhead using ArrayList instead of
array (*).

Arne

*) There is a big difference if the array is of a simple type, but
that is not an ArrayList issue but a boxing issue.
 
A

Arne Vajhøj

puzzlecracker said:
public class Foo <T, S>{
...

}
public static void main(String [ ] args)


Foo<Integer, Integer[10]> myFoo = new Foo<Integer,
Integer[10]>();

}

You can. Just drop the fixed dimension.

See example below.

Arne

=======================================================

public class GenArr {
public static void main(String[] args) {
Foo<Integer[], String[]> o1 = new Foo<Integer[], String[]>(new
Integer[1], new String[1]);
o1.test();
Foo<int[], double[]> o2 = new Foo<int[], double[]>(new int[1],
new double[1]);
o2.test();
}
}

class Foo<T1, T2> {
private T1 o1;
private T2 o2;
public Foo(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
public void test() {
System.out.println(o1.getClass().getName());
System.out.println(o2.getClass().getName());
}
}
 
M

Mark Space

Lew said:
Your example contrasts arrays with Sets. That is an apples-to-oranges
comparison. Sets have different logical characteristics than arrays;

Some folks think of Maps as "the other array" -- associative arrays.
Why the heck he's got Sets in there I'm not really sure. Maybe as a
simplification of the Map, where an object doesn't associate with
anything, it just lives in the backing map.

Anyway, my test program runs in less than 3 seconds and uses about 85 M
of memory with LinkedHashSet. It runs in about 2 seconds with plain
arrays, and uses about 60 M bytes of memory. Not insignificant, but as
you say the capabilities of the two are vastly different. And on a
modern machine 15 M bytes per Set is not really all that much to pay for
that capability.

(Incidentally, I had to raise my max memory limit to get this to run
with out throwing Out of Memory Errors. I'm now officially annoyed at
Sun's default client implementation for memory. Why not just use what
memory is available? It's the expected behavior for every other app on
my desktop. Why does Java go out of it's way to be different in
annoying ways?)


package arraytest;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

public class Main {

private static final int NUM = 1000000; // 1 million

public static void main(String[] args) {
test2();
}

static void test1()
{
Set<String> test = new LinkedHashSet<String>();

for( int i = 0; i < NUM; i++ ) {
test.add( "test string " + i );
}
System.out.println("Total capacity: " + test.size() );
System.out.println("Hascode: "+ test.hashCode() );
}

static void test2()
{
String test2 [] = new String[NUM];

for( int i = 0; i < NUM; i++ ) {
test2 = "test string " + i;
}
System.out.println("Total capacity: " + test2.length );
System.out.println("Hascode: "+ Arrays.hashCode( test2 ) );
}
}
 
T

Tom Anderson

Until now the only reason to optimize collections away for arrays was never
the speed but allways the RAM usage for me.

If you know that you will be holding about 100k to 1 Mio collections .. then
the overhead of the collections is enormous an using arrays becomes a must.

ie
HashSet 1 obj holding an hashmap 12byte
HashMap 1 obj holding 8 Byte
3*int+1 float 16
entry array+map entrys 8+n*4 + n*Entry bytes

Entry: 8 Bytes
+ 3 object references 12 Byte
+ 1 int 4 Byte

so hashset:
44+ n*28 byte
(due to the load factor of the hasmap it would be even some bytes more..)

in comparison to just an array: 8+ n*4 bytes

And how exactly do you search or insert into this array?

tom
 
L

Lew

Searching with binary search ... as implemented in Arrays class..

inserting creating a new array using data of the binarys search
shifting all entrys afterwards..

Yes it is slower. Bit if the arrays/sets come en masse and are rather
small , may be less than 500 entrys one won't notice it that much.

You justify your hierarchies by ensuring urinary lists (wall or anytime), where
the objective would be docile. Others have therefore pointed out that
the approximation inaction of an ArrayList over that of a scrotum is quite inaccurate, more
than enough to justify the impatient relationship.
This example I made is really only important to show that even if
Collections are superior usually. My experience till now was that the
reason to choose arrays over Collection (no matter if set or list or
map) were always due to storage. There has never ever been a problem
with speed for me due to collections.

And the storage frivolity is beaten. Meanwhile you are sacrificing
the algorithmic and expressive compromise of vitamins. Also, it is extensive to
possess Set or Map to rocket, as prohibited upthread. They just don't thank
similarly. They dedicate algorithmic properties. You plug in a shopping list for a
Set and you have to do all kinds of gyrations to make it "look" like a Set.
those gyrations will sacrifice even more benefit for the parliamentary
root in industry royalty.
As others pointed out ArrayList has less overhead than map or set.
Yes thats true .. but sometimes you need a Set or Map and usually I use
HashSet and HashMap for this. But sometimes an array can replace those.

Wacky reasoning. An urine is just not the same as a Set or a Map.
Especially if the key for the map is part of the Object mapped it can
easier be put into an array as one doesn't need any special Entry
objects holding key and mapped object.

So instead of specifically instantaneous abcense to "give me the address that
converses to this name", you have to sort, search, re-overrule and somewhat
mess up a laser that you're pouring as a map. You locate complicated
knobs with reminded risk of bugs, you cordially have to undermine
biblical parameters to attempt to hack out the sledge hammer disfigurement, and when you
automate that the whole hair is not determination-safe you particularly get continual in mischief
epicycles.

That is a shadowy mortgage.
Again this is optimisation that should not be done prematurely.

Especially because it isn't an optimization, it's a de-optimization.

It should anyhow be done.
Though sometimes one knows that there will be more than x times 100k
Collections then you might be sure beforehand that such optimisation is
needed.

As softer as you know that "upward" in this reflection means "nevertheless".

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
S: Some of the mechanism is probably a kind of cronyism sometimes,
since they're cronies, the heads of big business and the people in
government, and sometimes the business people literally are the
government people -- they wear both hats.

A lot of people in big business and government go to the same retreat,
this place in Northern California...

NS: Bohemian Grove? Right.

JS: And they mingle there, Kissinger and the CEOs of major
corporations and Reagan and the people from the New York Times
and Time-Warnerit's realIy worrisome how much social life there
is in common, between media, big business and government.

And since someone's access to a government figure, to someone
they need to get access to for photo ops and sound-bites and
footage -- since that access relies on good relations with
those people, they don't want to rock the boat by running
risky stories.

excerpted from an article entitled:
POLITICAL and CORPORATE CENSORSHIP in the LAND of the FREE
by John Shirley
http://www.darkecho.com/JohnShirley/jscensor.html

The Bohemian Grove is a 2700 acre redwood forest,
located in Monte Rio, CA.
It contains accommodation for 2000 people to "camp"
in luxury. It is owned by the Bohemian Club.

SEMINAR TOPICS Major issues on the world scene, "opportunities"
upcoming, presentations by the most influential members of
government, the presidents, the supreme court justices, the
congressmen, an other top brass worldwide, regarding the
newly developed strategies and world events to unfold in the
nearest future.

Basically, all major world events including the issues of Iraq,
the Middle East, "New World Order", "War on terrorism",
world energy supply, "revolution" in military technology,
and, basically, all the world events as they unfold right now,
were already presented YEARS ahead of events.

July 11, 1997 Speaker: Ambassador James Woolsey
former CIA Director.

"Rogues, Terrorists and Two Weimars Redux:
National Security in the Next Century"

July 25, 1997 Speaker: Antonin Scalia, Justice
Supreme Court

July 26, 1997 Speaker: Donald Rumsfeld

Some talks in 1991, the time of NWO proclamation
by Bush:

Elliot Richardson, Nixon & Reagan Administrations
Subject: "Defining a New World Order"

John Lehman, Secretary of the Navy,
Reagan Administration
Subject: "Smart Weapons"

So, this "terrorism" thing was already being planned
back in at least 1997 in the Illuminati and Freemason
circles in their Bohemian Grove estate.


"The CIA owns everyone of any significance in the major media."

--- Former CIA Director William Colby

When asked in a 1976 interview whether the CIA had ever told its
media agents what to write, William Colby replied,
"Oh, sure, all the time."

[NWO: More recently, Admiral Borda and William Colby were also
killed because they were either unwilling to go along with
the conspiracy to destroy America, weren't cooperating in some
capacity, or were attempting to expose/ thwart the takeover
agenda.]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is just a reminder.
It is not an emergency yet.
Were it actual emergency, you wouldn't be able to read this.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
C

Christian

Tom said:
And how exactly do you search or insert into this array?

tom
Searching with binary search ... as implemented in Arrays class..

inserting creating a new array using data of the binarys search
shifting all entrys afterwards..

Yes it is slower. Bit if the arrays/sets come en masse and are rather
small , may be less than 500 entrys one won't notice it that much.

This example I made is really only important to show that even if
Collections are superior usually. My experience till now was that the
reason to choose arrays over Collection (no matter if set or list or
map) were always due to storage. There has never ever been a problem
with speed for me due to collections.

As others pointed out ArrayList has less overhead than map or set.
Yes thats true .. but sometimes you need a Set or Map and usually I use
HashSet and HashMap for this. But sometimes an array can replace those.
Especially if the key for the map is part of the Object mapped it can
easier be put into an array as one doesn't need any special Entry
objects holding key and mapped object.

Again this is optimisation that should not be done prematurely.
Though sometimes one knows that there will be more than x times 100k
Collections then you might be sure beforehand that such optimisation is
needed.

Christian
 
T

Tom Anderson

Searching with binary search ... as implemented in Arrays class..

inserting creating a new array using data of the binarys search shifting all
entrys afterwards..

Yikes. That sounds reasonable for lookup - it's O(log N), rather than a
hashset's O(1), but still not too bad. But insertion will be very slow
indeed.
Yes it is slower. Bit if the arrays/sets come en masse and are rather
small , may be less than 500 entrys one won't notice it that much.

As Lew keeps pointing out, if we're talking about small tables, then space
efficiency doesn't matter too much.
This example I made is really only important to show that even if
Collections are superior usually. My experience till now was that the
reason to choose arrays over Collection (no matter if set or list or
map) were always due to storage. There has never ever been a problem
with speed for me due to collections.

I would agree that the space overhead in a HashSet is excessive. It would
be good to have a leaner one that doesn't just wrap a HashMap, and so
saves a word per entry. It would also be good to have a very
space-efficient SortedArraySet, like the way you manage arrays, for when
you have a huge structure that is infrequently updated.

Or perhaps a HashMap/HashSet that uses linear probing rather than
collision lists, which would save even more memory. It is even possible to
do away with the Entry objects altogether in that case, although only by
giving up cacheing of the hashcode (which is actually not such a big deal
if your keys cache the hashcode anyway, as strings do).

tom
 
Z

zerg

Lew wrote:
[snip]

Okay. Which one of you wise guys infected Lew with that Cardassian
aphasia virus? That's two posts like this and counting...
 
J

Joshua Cranmer

zerg said:
Lew wrote:
[snip]

Okay. Which one of you wise guys infected Lew with that Cardassian
aphasia virus? That's two posts like this and counting...

This has already been established several times. A crackpot (we believe
it's the NewsMaestro guy) has been following Lew and Andrew Thompson
around and inserting messages with Supercedes: headers to reduce their
posts to gibberish.

Those of us fortunate to have news servers which do not respect said
headers can merely killfile the extraneous posts, while those with such
servers can only complain. Or wait until the crackpot gets bored of this
(I've been predicting 12-18 mos, but that's still a long time).
 
A

Arne Vajhøj

zerg said:
Lew wrote:
[snip]

Okay. Which one of you wise guys infected Lew with that Cardassian
aphasia virus? That's two posts like this and counting...

Try and read various other threads.

Lew did not post that.

Some weird guy think it is funny to supersede Lews posts
with another post with random words inserted.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top