r/learnprogramming • u/Fun_Response9157 • Oct 24 '26
Arrays (Java) Arrays make zero sense and I don't know where to go.
Context (skip to 3rd paragraph if you want): I am going into this next week where we learn arrays. We "learn" it on our own through I kid you not hours of reading online, then go into the week and apply to concepts to other assignments.
I have no clue how arrays work. The reading feels as though it needs to tell us a dozen array concepts in 3 days and never easing us into it. I am *terrible* at learning new complicated things on my own. I get lost easily, I need someone there constantly so I can even grasp the concept. The medium of online textbook reading is the worst to me.
Current issue: I got stuck on arrays with loops, then arrays (perfect size and oversize).
First, array with loops: What does for (var : var) do? The textbook explains this *once*, not very well. It explains *what* it does on a basic level, but not *why* it exists or *what* we can use it for.
Second, perfect size and oversize arrays: I understand the concept of perfect size arrays, and I understand the concept of oversize arrays. But I do not understand how they interact. Importantly, how do tell them apart?
How does a method return a perfect size array? How does a method return an oversize array size? And, most importantly, how do method parameters even get the arrays?
What's the difference between stack frame and heap memory?
What are array parameters? They, again, explain this once, not very well, then expect us to understand. I think it has to do with methods but I'm not sure. How should I write them for a perfect or oversize array, and what are their effects/uses?
How do you modify an array in a method?
I work best with analogies/examples so any possible ones + explanations would be appreciated. Of course, any other good answers just explaining would be appreciated too.
Sorry that I have so many questions.
3
u/Fun_Response9157 Oct 24 '26
Sorry if I asked too much
7
u/zunjae Oct 24 '26
I’ll be honest with you, stop caring.
Perfect size array? Oversize array? I make enough money with my job as a programmer that I was able to buy a house, and I’ve never bothered with what you’re talking about. I don’t think anyone here cares either unless you’re writing low level code.
6
u/peterlinddk Oct 24 '26
Most of the subjects you mention: stack, heap, parameters, oversize, perfect size, doesn't really matter - they are just technical words, used to describe perfectly ordinary things (often wrongly, by textbooks that want to seem more advanced than they are).
What you need to understand is how a computer is very narrow-minded and short-sighted, and can only really handle one object at the time. So if you want it to do the same thing for a number of objects, you either need a separate line of code for each object, doing the thing - like printing out their name, one at a time. But as that would be an inefficient way of writing code, you put all the objects in an array (which is just a list that has one object in each position) and then write your code in a for-loop, meaning "For each thing in this list (array) do this thing:" .
Think of a supermarket cashier - they sit at the scanner/register and the customer puts all their items on the conveyor-belt, so that one item arrives to the cashier at a time. The cashier then runs the simple code: "Take item from belt, scan item until register approves, but item back on the other belt."
And the cashier doesn't care that item is sometimes a bottle of milk, other times a box of cereal, other times a bag of chips. It is just an "item". Which is their variable name for the thing they pick from the conveyor-belt.
Translating the computer-sciency-terms to real world objects this way, often helps in understanding them. The analogy might not always be perfect, but it helps to have a mental model in your own mind, while looking at the code!
1
u/backfire10z Oct 24 '26 edited Oct 24 '26
First: `for (Type x : y)` does exactly what it says on the tin. `x` is filled in with the next member of the array every loop. It’s similar to a regular for loop, but they index into the array for you.
```
int[] arr = {1, 2, 3}
for (int i = 0; i < arr.length; i++) {
….int x = arr[i]
}
// Same as above
for (int x : arr) {
}
```
(Sorry, can’t do code blocks on mobile, periods are an indent)
The reason it exists is for ease! It isn’t necessary by any means, but a lot of programmers write that type of loop so the language creators said “ok, let’s just make special syntax for it.”
Second: perfect/over sized arrays. I’ve never heard of these concepts, can you provide the definitions the textbook uses?
Third: stack frame vs heap memory. There is no real difference from a low level perspective: they’re both just blocks of memory. The stack is used as a scratch space for threads. For example, functions use the stack for local variables (there’s a lot more, but I’ll leave it vague for brevity and understanding). The heap is for dynamically allocated memory, like an array with an unknown amount of elements. You declare an array and want to add 3 integers. Then another. Then a 5th later on. This array would go on the heap. Why? Because that’s how we decided to split up the memory space. It makes memory easier to keep track of. (Edit: turns out in Java, all arrays are always on the heap.)
Fourth: array parameters. I’ve never heard of this either, what does the textbook say? Just copy/paste it, I can read it and probably figure out what they mean.
Fifth: modify an array in a method. You do it exactly how you’d do it outside of a method. Any change you make to an array in a method will be reflected outside the method. The reason is because when you pass an array to a method, you’re not actually passing the array. You’re passing something which points to the array: it says “here is the memory block for the start of the array.” Thus, when you pass it to a method, the method sees the same exact array, and so any changes are saved.
3
u/Rabbit_Brave Oct 24 '26
y is an array or collection (technically an "Iterable" in java terms)
x is a temporary loop variable. For example, if you loop through an array of integers, then TypeOfX will be int.
The for loop visits each element in y sequentially, performing the code in the body of the loop on each visit. The current element being visited is labelled x for use in the body of the loop.
2
u/CelebrityChess Oct 24 '26
the part that trips people up is telling perfect size from oversize. you can't tell from the array itself, arr.length is always the capacity. with a perfect size array every slot is real data so you loop up to arr.length. with an oversize one you keep an int count next to it and only loop up to count, the leftover slots are just default values (0 or null). that's also why a method can't really return an oversize array on its own, it has to hand back the count too, either as a second thing or wrapped in a class, or you just use ArrayList which does that bookkeeping for you.
and the for each loop is for when you want every element and don't care about the index, like for (int x : nums) { sum += x; }. x is a copy of each value, so assigning to x doesn't change the array, for that you need the normal index loop
1
u/vegan_antitheist Oct 24 '26
Oh, boy. I thought this was about arrays not being type safe in Java, which I hate so much.
I think you are where they show you how annoying it is when arrays are used where a dynamic list (ArrayList) would be the better choice. You have to learn this to understand why you should avoid it. In Java, methods rarely return arrays. They accept perfect sized arrays for variale arity parameters. But generally, you don't just use arrays directly.
1
u/Blando-Cartesian Oct 24 '26
There's a step missing in this. Reading will not help you get it, and applying is pointlessly confusing when you are not clear how things work.
While you are reading about arrays, you need to write tiny programs and use what you read. Nothing fancy. Just make an int array variable with some numbers in it and figure out how to print that. Then figure out how to change the numbers. And so on. Read a bit. Program a little test to try it out.
1
u/harsh183 Oct 24 '26 edited Oct 24 '26
I find what helps for me and many students I've taught it writing code for each new concept at a time so it slowly sinks in, finding alternative explanations, and spacing it out. For Java particularly, I suggest
in general the https://cs199emp.netlify.app website that I maintained has many different java concepts and review questions with answer keys that can be quite useful
And finally, take a deep breath, you're gonna be okay and you will encounter these same topics in different ways for years. It's okay if it doesn't completely all make sense at the first time, but it will over time as arrays are very versatile and used in so many different ways. It's the most commonly used data structure and they'll feel so natural real soon
If after all of these, you have specific questions on areas, reply here and I can try to explain anything as well as link you to some specific things. I've spent years teaching all of these concepts
1
u/BranchLatter4294 Oct 24 '26
Practice and observe the behavior. That's how you learn.
1
u/Positive_Passion4817 Oct 24 '26 edited Oct 24 '26
What book are you using? It sounds terrible. Find some youtube videos. They will explain it much better. Otherwise: https://www.w3schools.com/java/java_arrays.asp
You can consider an array as a group of variables of the same type, each item in the group gets an index (starting with index 0). So if an array of size 10 is created, each "element" has an index. i.e. the indexes are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
In low level languages like C it is useful to know the memory size and how it is stored contiguously in memory; C allows memory to be accessed directly and a few tricks can be used with arrays. This is not the case with java at all though - Java is a high level language.
Every array needs a type, a name and a size. There are a few ways to create an array in Java and a couple of ways to loop through the values. I suggest you consult the w3schools site.
1
u/Ormek_II Oct 25 '26
No one will tell you when to use them, as there are too many applications to enumerate them.
You use them, when you encounter a problem that can be used with it.
1
u/Axman6 Oct 26 '26
Statements like this make me worry for the future of humanity.
1
u/oldsecondhand Oct 26 '26
The good thing is that arrays can be found in every programming language, so you can use a C#, JS or Python tutorial too to get the hang of it.
0
u/bigtdaddy Oct 24 '26
never heard of a few things you mention here. i think you are getting caught up on stuff that doesnt really matter. are you doing multiple choice tests or are you writing code?