This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action 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

How to add integers to an array?

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a for loop which outputs a bunch of numbers but I'd like to save the numbers in an array.


How can I save mixArray[i] into an Array called newArray?

Thanks so much!
 
Marshal
Posts: 70684
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need a new array declared before the loop, and copy the numbers into it. That is if you insist on doing it the hard way. Otherwise find the methods in the Arrays class that will do the copying for you.
The name newArray in line 3 is very misleading.
 
Glenda Karen
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much! I will try looking for a method in Array class.
Good point, I'll change the name.
 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Glenda Karen wrote:Thanks so much! . . . .

That's a pleasure There are many methods with names starting copy; maybe copyOfRange() will work. As an alternative, there is the old arraycopy() method in the System class.
 
reply
    Bookmark Topic Watch Topic
  • New Topic