Java programing help

noagname

Senior member
Jan 4, 2006
295
0
0
class arrayLearn
{
public static void main(String args[])
{
//variables
int[200] elfSeniority;

//before changes
System.out.println("Before: " + elfSeniority[193]);

//change
elfSeniority[193] += 1;

//final result
System.out.println("After: " + elfSeniority[193]);
}//main
}//class
___________________________________________________
when trying to compile
C:\Users\Ateev\Desktop\arrayLearn.java:6: ']' expected
int[200] elfSeniority;
^
C:\Users\Ateev\Desktop\arrayLearn.java:6: not a statement
int[200] elfSeniority;
^
_____________________________________________________


Now i am just starting out in learning java.
i am having trouble learning arrays so my brother made this for me but i am not able to compile it, do you know what is wrong with it?

even without compiling this i am still not able to understand arrays?



 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Instead of:
int[200] elfSeniority;
use:
int[] elfSeniority = new int[200];
 

stevf

Senior member
Jan 26, 2005
290
0
0
yes - that will fix that program so it will compile.

What dont you understand about arrays? perhaps if you try to explain what you dont understand we can help you understand.
 

noagname

Senior member
Jan 4, 2006
295
0
0
first: thanks for your help
second: well i think my question is what are arrays meant to do?
 

stevf

Senior member
Jan 26, 2005
290
0
0
arrays make it very easy to deal with variables that are the same type and all related. For instance, lets say you have 20 students in a class and each student receives 50 grades. how many variables would you need to declare and use to store each students grades. With an array you only need one variable.

Also you can use loops with arrays to manipulate all the data in the array. That is very hard to do with discrete variables as you have to code all variables.

For example

variable1 =x, variable2 =x, variable3 = x, ... , variable10=x
if you want to sort these variables, print them, etc. you must type them every time and the code gets longer and longer as in

print variable1;
print variable2;
etc


but with an array

variable[10] = x

so the non-array example variable1 = x, would become variable[0] = x, etc. just about all programming languages arrays start at [0] not [1] and the last index for the array is the number you specify when you create the array minus one. unfortunately, this can be confusing at first

you can use a loop to print them or sort them, etc, as in

for (i =0; i <10; i++){
print variable;
}

that is a lot less code than having a seperate print statement for each variable.

Does that help explain it


 

noagname

Senior member
Jan 4, 2006
295
0
0
yea
slightly confusing but after reading once or twice i got it
thanks you

just wondering how many years have you done java
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Arrays are just used to store and work with a collection of things. They can be a bunch of integers, a bunch of strings, characters, or even mixed. Like stevf said, they are helpful when you're dealing with a bunch of "something".
 

stevf

Senior member
Jan 26, 2005
290
0
0
I am relatively new to java and am still studying it, but have done some amout of programming since the early 80's. Arrays exist in just about every programming language, not just java, so the concept remains the same regardless of the language
 

noagname

Senior member
Jan 4, 2006
295
0
0
i have spent a ton of time trying to figure this out but in this program
http://www.cadenhead.org/book/...pter09/Find40013s.java
i do not under stand how this works
thirteens[numFound] = candidate;

is it taking the array and giving "numFound" 400 spaces. then "numFound" puts a number in their and then "candidate" is equal to that number.

if i am right which i dont' think so what happens after that

 

mlm

Senior member
Feb 19, 2006
933
0
0
Originally posted by: noagname
i have spent a ton of time trying to figure this out but in this program
http://www.cadenhead.org/book/...pter09/Find40013s.java
i do not under stand how this works
thirteens[numFound] = candidate;

is it taking the array and giving "numFound" 400 spaces. then "numFound" puts a number in their and then "candidate" is equal to that number.

if i am right which i dont' think so what happens after that

int[] thirteens = new int[400];
creates an array of ints with 400 "spaces"

numFound acts as an index which starts as 0 and stops at 399. This index retrieves whatever data is located in that space, and the line you posted will assign that data to "candidate".

So thirteens will end up with 13, 26, 39, ...
 

stevf

Senior member
Jan 26, 2005
290
0
0
yes that looks like what that does - it puts the 13 in element 0, 26 in element 2, etc then it prints them all out
noagname - when you deal with arrays in java, the name of the array comes first and whatever is inside the [] refers to the element - that value can be a number, a variable, or even a math expression, such as array[x/2] - assuming you have an array called array and a variable called x - the expression can be more elaborate than that


maybe this analogy will help you - think of a street as an array and each house is the elements in the array. each house has an address and each house can hold something. so when you need something from a house you have to go to the street then the address

i.e on main street we have #1 main st, #2 main st ...... down to whatever the end is

to describe this street as an array you would have mainStreet[houseAddress]

so if your friend lived at #5 main st you would go to mainStreet[4] - remember the array starts at 0. or you you could use mainStreet[myFriendBob-1] (the -1 is because the array starts at 0 again) and elsewhere you list int myFriendBob = 5
 

ppdes

Senior member
May 16, 2004
739
0
0
Collection implementations are preferred anyway. If you want the data ultimately stored in an array, just use an ArrayList which will do so and offer you extra functionality to boot.
 

dealmaster00

Golden Member
Apr 16, 2007
1,620
0
0
Originally posted by: ppdes
Collection implementations are preferred anyway. If you want the data ultimately stored in an array, just use an ArrayList which will do so and offer you extra functionality to boot.

No, they are not "preferred," it depends on what you need it for.
 

piasabird

Lifer
Feb 6, 2002
17,168
60
91
I think typically I would define an array as:
int name[200];
This is an array of integers size of 200. However, after the definition the array has no values. You have to initialize it first. When you define the array you are only defining a pointer to 200 locatations in memory. Therefore you can not add anything to "".

Might initialize it like so:
for (x = 0; x < 200; x++)
{ name[x] = 0; }

name is the array name.
x is a pointer
Value of all array elements are zero.
In C++, the first pointer is zero.
Since the size is 200, then the last entry is name[199].
name[199] is the location and the value is zero.

#include<iostream.h>
void main()
{

int name[200];

for (x = 0; x < 200; x++)
{
name[x] = 0;

cout<<"\nname["<<x<<"] = "<<name[x]<<"\n";
}

}


This might work. I have not done any C++ in a while.
 

stevf

Senior member
Jan 26, 2005
290
0
0
java automatically initializes all elements in an array to zero so you just have to declare the array, the loop isnt needed to initialize all elements if zero for each element works for you.

Also java added an enhanced for loop for arrays for when you need to access all elements in the array. That looks like:

for (type variablename : arrayname)
{ }

ie - to print the array you would use something like

int[] testArray = new int[100];
for (int value : testArray)
{
system.out.print(value + " ");
}
this is also called an foreach loop
 

ppdes

Senior member
May 16, 2004
739
0
0
Originally posted by: dealmaster00
Originally posted by: ppdes
Collection implementations are preferred anyway. If you want the data ultimately stored in an array, just use an ArrayList which will do so and offer you extra functionality to boot.

No, they are not "preferred," it depends on what you need it for.

You don't seem to understand what the word preferred means. If the situation requires one or the other than preference doesn't matter. If your current requirements simply require for array based storage, however, then a Collection backed by an ArrayList is preferred over an array by anyone who understands OOP.

The time complexity is the same due to the same backing data structure. You will be able to add, remove, sort, check the contents, and many other operations much more easily, however. You can also drop a linked list or set in later if needed without rewriting all your code. It makes your code more maintainable, use more already tested library code, easier to add functionality to, and more decoupled from the actual data structure if that ever needs to be changed.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |