Newbie Java array question

Status
You're currently viewing only Blacken00100's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Unless you know the exact size of your working set, you shouldn't use arrays at all. This is that ArrayList<T> is for. Java arrays conform to the traditional "array" concept (in interface and generally in implementation); they are of static size. Most frequently, languages with "dynamic arrays" don't have arrays at all, but other constructs "helpfully" mislabeled as arrays. PowerShell is one of these.

Be careful of assuming anything based on a shell scripting language. Also, your book sounds like a piece of shit; I'd go Thinking in Java or something (it's freely available online).

(Aside: you may find yourself pissing people off if you write statements like your quoted bit. You can get away with that tone when you know half a dozen languages well enough to write production code, but before then it sounds "precocious," and "precocious" is just a fancy word for "douchey." Understand that your classmates, if not you yourself, cannot be relied upon to possess the fundamentals necessary for your instructor to provide a meaningful answer. Smells show-offy.)
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
I don't know if I'd go that far. The array construct is a valuable one, and isn't really a "primitive" at all (it is a hilariously horrible hack but it isn't a primitive).

If you need to resize an array in the normal course of programming, however, that should be something of a warning flags. Most shitty Java books come from programmers who are C++ programmers first and Java programmers second, which is why stuff like "deep copying arrays" is covered at all.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Faramir":2ci3jkgv said:
The point is that in those situations you shouldn't be using an array. Rather you probably want some kind of list.

Edit: beaten

Re: primitives
I guess the syntactic sugar made it look primitive. I had no idea it was implemented in such a hacky manner.
Dude, Java in general is that way. It is seriously amazing that it works as well as it does. The guy in the next cube over is a former JVM hacker (has a JSR still pending for Java 7) and the only time I've ever seen this hippie-ish dude get mad is talking about the code in javac.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Bagheera":qbk6l9xm said:
Blacken00100":qbk6l9xm said:
If you need to resize an array in the normal course of programming, however, that should be something of a warning flags.

Really? What if you're querying multiple tables in a database, or grabbing event logs from multiple servers, or grabbing looped input from a user? In each case, you don't know how many elements (how many rows, events, or inputted items) you'll eventually get. Is that a warning sign or just typical programming?
Yes. Really. (You would have done better to pick up on my mention of ArrayList<T> and investigate Java containers instead of getting snippy.)
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
newwb":1lipscrs said:
Blacken00100":1lipscrs said:
The array construct is a valuable one,
Yes, in that it allows ArrayList to be implemented in terms of it. It's rare that you need to use arrays (directly). You should virtually always use collection classes.
Sure - at least, for most developers. (It's why I mentioned ArrayList in the first place.)

and isn't really a "primitive" at all
Except that you can't implement it from scratch in Java code, so in that sense it's primitive. Conceptually, it's primitive because it's low-level.
"primitive" in that sense is really squishy, though. I mean, "int" is a primitive. Literally. Arrays, not so much. But that's just semantics.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
vafarmboy":1w403565 said:
Relax, kids. He's in an intro class. He's got to learn what an array is and its limitations are before he can blithely use all the happy fun collection stuff that makes them not totally suck to use. I mean, who wouldn't teach manually managed memory in a language where you're not supposed to have to manage memory manually?
Eh - you're not "manually managing memory," at least not in the sense his instructor is (probably) driving toward. An array is, IMO, a very conceptually simple data structure, and starting there isn't the worst thing in the world.

(His making himself look like a bit of a douche by assuming things based on information he does not possess is probably more concerning. Not too awesome for someone in an intro class.)
 
Status
You're currently viewing only Blacken00100's posts. Click here to go back to viewing the entire thread.
Not open for further replies.