File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Confusion with Arrays: Where are they stored? Why not manually update arrays all at once? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion with Arrays: Where are they stored? Why not manually update arrays all at once?" Watch "Confusion with Arrays: Where are they stored? Why not manually update arrays all at once?" New topic
Author

Confusion with Arrays: Where are they stored? Why not manually update arrays all at once?

Anthony Allen
Greenhorn

Joined: Jan 08, 2013
Posts: 3
I've watched tutorials and read books on arrays in Java. And one thing that confuses me is they go through and show you how to add to an array and how to create one. But after you create one, do you just continuously have to write code to add to them?

Maybe my HTML/web design knowledge is interfering. But I would think there is a file somewhere in your java software files that you could go to and open like a text file. And just manually copy your entire array files in there.

For example, if I was going to make a dictionary website on some subject. Do I really have to write out thousands of lines of code to add to array for each dictionary term and string definition? Or can I just import some sort of text separated file? Similarly, what if it was a massive image site and I got 100,000 images in a folder on my server.

That's where I am confused with arrays. Can anyone clear this up for me? Would really appreciate it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

Anthony Allen wrote:But after you create one, do you just continuously have to write code to add to them?

When you create an array with new, you get an empty array; if the type of the elements is a primitive type such as int, all elements will have the default value for the primitive type (which is 0 in case of int), if it's an array of objects then all elements will be null. You'll want to put values in the array.

If you have data in a file that you want to put the the array, you'll have to write some code to open the file, read it and put the data in the array. It doesn't matter how many items there are in the file though, you'd just write a loop to read however many items there are - it's not so that you'd have to write a line of code for every single item in the array.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Anthony Allen wrote:I've watched tutorials and read books on arrays in Java. And one thing that confuses me is they go through and show you how to add to an array and how to create one. But after you create one, do you just continuously have to write code to add to them?

Maybe my HTML/web design knowledge is interfering. But I would think there is a file somewhere in your java software files that you could go to and open like a text file.

Arrays are held in memory, not on disk, so no, you can't just change a text file to get the array to change. And yes, your HTML experience is getting in the way. Most programming languages are not simple markup for static text.

You can program it to work this way - you would have to open a the file, read the contents of the file and fill the array with its contents, for example.

For example, if I was going to make a dictionary website on some subject. Do I really have to write out thousands of lines of code to add to array for each dictionary term and string definition? Or can I just import some sort of text separated file?


Go the second route. And you probably don't want to an Array, you want something more flexible - maybe a Map. You should define the text format you want then find a library which handles that format to read the file into something that can be accessed in memory or through a simple API.

You might find an API which does the entire dictionary thing for you...

Similarly, what if it was a massive image site and I got 100,000 images in a folder on my server.

That depends a lot on what you want to do with them. You probably don't need or want to hold them all in memory at once. So all you need to do is either hold a reference to their file or create mechanism where you can calculate the file you want to reference based on something else...

That's where I am confused with arrays. Can anyone clear this up for me? Would really appreciate it.

I think this isn't really a problem with arrays, it is a confusion with programming language versus a markup language. You probably have to go back to the start and try to learn it without trying to put it in the context of HTML - because it is nothing like that at all.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion with Arrays: Where are they stored? Why not manually update arrays all at once?