This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Arrays - assign values

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have this code:



When I run it, it outputs : [Oil News, null, Humans: Exterminate Or Not?, Organic Eye Implants, null, null, null, null, null, null], which is correct,

however, what I don't understand is , why when I write : newArticle = favoriteArticles[favoriteIndex] , it outputs :

[null, null, null, null, null, null, null, null, null, null]
 
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Itsumo Snow wrote:why when I write : newArticle = favoriteArticles[favoriteIndex] , it outputs : [null, null, null, null, null, null, null, null, null, null]


"newArticle" is a variable local to the method, modifying it doesn't change anything outside of the method. "favoriteArticles[favoriteIndex]" is null because it hasn't been set yet.
Content minimized. Click to view
 
Carey Brown
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Itsumo Snow wrote:why when I write : newArticle = favoriteArticles[favoriteIndex] , it outputs : [null, null, null, null, null, null, null, null, null, null]


"newArticle" is a variable local to the method, modifying it doesn't change anything outside of the method. Modifing it DOES clobber what value was passed in as a parameter. "favoriteArticles[favoriteIndex]" is null because it hasn't been set yet.
 
Bartender
Posts: 2697
130
Google Web Toolkit Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all ... Welcome to coderanch !! We hope you have an amazing time here in your quest to learn coding !!

I think your program can explain itself. I have added a few print statements to your method setFavoriteArticle :
I suggest running this and letting us know if the output makes sense ;)
 
Itsumo Snow
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:First of all ... Welcome to coderanch !! We hope you have an amazing time here in your quest to learn coding !!

I think your program can explain itself. I have added a few print statements to your method setFavoriteArticle :
I suggest running this and letting us know if the output makes sense ;)

Hi!, thank you! I ran it and it explains well that the values at the given indexes are null and because of that the value of newArticle is changed to "null". Thank you so much for explaining this, I know it's a very stupid question, I just started learning java and my questions are silly.

 
salvin francis
Bartender
Posts: 2697
130
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Itsumo Snow wrote:... Thank you so much for explaining this, I know it's a very stupid question, I just started learning java and my questions are silly.


Glad to help  
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic