Jump to content

JAVA : Question about arrays

So in my textbook, it shows array initialization as: int[] arrayName = new int[x]; but I have been doing it like this: int arrayName[] = new int[x]; is there a difference, does it matter?

Also it shows copying an array reference without using []. this is exactly as it shows in my book

int[] scores = {10, 9, 7, 4, 5};

int[] values = scores; //copying array reference

 

i thought you had to use [] when referencing an array, like this:

int values[] = scores[];

 

Can someone clarify for me? I'm a learner for java.

Link to comment
Share on other sites

Link to post
Share on other sites

You don't need a [] because it's the variable name of the array and it's referencing the array.

If you still use it, your program won't compile anyways.

You only need an [] after the variable name if you want to get a specific value with an index in your array.

int value = scores[index];

 

Link to comment
Share on other sites

Link to post
Share on other sites

int arrayName[] = new int[x]; 

is a syntax used in C and works for whatever historical reason. I would advise to only use

int[] arrayName = new int[x];

as it makes more sense from a java perspective. 

Link to comment
Share on other sites

Link to post
Share on other sites

Keep in mind, you should almost always avoid using arrays like that and instead use some collection like List or Set.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Also remember that when you want to copy without using a reference you have to use the function clone or a for loop.

5 minutes ago, reniat said:

Keep in mind, you should almost always avoid using arrays like that and instead use some collection like List or Set.

This is good advise, use the simple array type is ok for learning but to actually make something real it's a pain because for example you can't easily find the length. Collection are much nicer to use and have all sorts of useful functions that will same you a lot of time.

Gaming Rig:CPU: Xeon E3-1230 v2¦RAM: 16GB DDR3 Balistix 1600Mhz¦MB: MSI Z77A-G43¦HDD: 480GB SSD, 3.5TB HDDs¦GPU: AMD Radeon VII¦PSU: FSP 700W¦Case: Carbide 300R

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×