GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Assigning elements of an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning elements of an array" Watch "Assigning elements of an array" New topic
Author

Assigning elements of an array

Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
I have this assignment.
Given:


I've tried the following to satisfy the assignment, without success:




Thank you, in advance, for any help.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Those codes that you tried violate java syntax on arrays.

I encourage you to look at this link:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

and for starters, imitate how it handles arrays.
Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
Originally posted by Jesus Angeles:
Those codes that you tried violate java syntax on arrays.

I encourage you to look at this link:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

and for starters, imitate how it handles arrays.


Thank you, Jesus.
Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
Originally posted by Oscar E Rodriguez:


Thank you, Jesus.


Well I read through the java tutorial...and still can not get this to work.

What is confusing me is how the

affects the way I assign element 3.
I also can't find any reference in books or on the net that describe the


I understand the above code allocates memory for 10 integers.

But I can not figure out the syntax to initialize the elements.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
I am also not sure why it said "assign t=4".

It could mean most likely, that you need to set t first to 4. This can be done in the variable declaration:



or in the code



After you set t to 4, you can assign t into the 3rd item in the array by:

arr[2]=t;

Let us know if you dont understand why these are the answers.
Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
Originally posted by Jesus Angeles:
I am also not sure why it said "assign t=4".

It could mean most likely, that you need to set t first to 4. This can be done in the variable declaration:



or in the code



After you set t to 4, you can assign t into the 3rd item in the array by:

arr[2]=t;

Let us know if you dont understand why these are the answers.


What if the professor mistyped the assignment and meant to say:
"assign element 3 of array arr the value of 4"?
In this case why can't I just assign it like this:

in the class ArrayDemo in the tutorial that you recommended to me, they do that...why does it not work in this case?

I really appreciate all your help on this.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Exactly. You can do it like that, if that is what the professor was trying to say.
Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
Originally posted by Jesus Angeles:
Exactly. You can do it like that, if that is what the professor was trying to say.


Cool!
thanks Jesus...you've truly been my savior on this assignment (pardon the pun, couldn't help it).
I feel like I understand this so much better now. Thanks, again.
Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
Originally posted by Jesus Angeles:
I am also not sure why it said "assign t=4".

It could mean most likely, that you need to set t first to 4. This can be done in the variable declaration:



or in the code



After you set t to 4, you can assign t into the 3rd item in the array by:

arr[2]=t;

Let us know if you dont understand why these are the answers.


So I clarified with my professor the assignment.
He said:
"1) Create an instance of 'A' that will be referenced from element 3 (which can be either arr[2] or arr[3] depending on how you interpret the question).
2) Assign a value of 4 to the instance variable 't' of that newly created instance of 'A'."

Given that, I figured to implement the above example you gave me. But when I try to compile it I get the following error:
Exam8.java:5: non-static variable t cannot be referenced from a static context
t=4;
^
Exam8.java:9: non-static variable t cannot be referenced from a static context
arr[2]=t;

I looked uyp the error and the only fix I could find was to remove "static" from the main. But I don't think that is what my professor wants.

Any ideas?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

I looked uyp the error and the only fix I could find was to remove "static" from the main. But I don't think that is what my professor wants.


No. This change will allow your program to compile, but it will not longer run.

BTW, I think you are overthinking the solution. You professor practically spoon fed you the answer -- he gave you the answer in english, instead of in Java.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
Originally posted by Henry Wong:


No. This change will allow your program to compile, but it will not longer run.

BTW, I think you are overthinking the solution. You professor practically spoon fed you the answer -- he gave you the answer in english, instead of in Java.

Henry


I know he's spoon feeding it to me, that is what if frustrating me. I thought I understood it until he "clarified" it for me. If I am being spoon fed I am the baby with the food all over his face and none in his mouth...
I'm just not getting it.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
'main' is a static method. 't' is not a static variable. Static methods can only access fellow static methods and variables.

The code I gave you are chunks to give you an idea. However, what I posted is probably a little bit misleading for a beginner. You cannot do a t=4 in the main() method because of the 'static' issue. You have to set t on an instance of A, like:

A a=new A();
a.t=4;
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Originally posted by Jesus Angeles:
You have to set t on an instance of A, like: . . .


Far better solution than what you sometimes see, where they suggest setting the "t" static
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Assigning elements of an array