• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array range of numbers

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I would like to put a range of numbers from 1 - 1000 into an array but, i can not number from 1 to 1000
example

int[] number = {1..1000};



i tried using


but i does not work. It telling me i can not convect to an integer
I also tried this


But i can not assign it to a particular array variable to use.

All i want is to have something like this //array of list to 1000
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Have a look at some array examples to make sure that number = i is the correct way to set a value.

In your second example, there is actually a toArray() function for array list which would give you the ability to do what you want to do.
ArrayList toArray()
 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Molayo Decker wrote:
but i does not work. It telling me i can not convect to an integer


error might be : cannot assign an array to an integer
because number is an array variable which can hold 1000 in primitive values. whereas i itself is a primitive value. So, how could you thing of assigning a primitive value to an array variable is valid. you can only assign an array of integer to the number (only integer)
like this,

now this is valid and number will refer to an array of integer which can hold 500 integer primitive values.

hope it helps
Puspender
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not trying to create another array that can hold 500 primitive integers. I'm trying to find a way to create an array that "CONTAINS" "NOT HOLD" primitive integers from 1 - 1000.
So that i can have an array like . Hence i can point the number "2" in the array like number.length[1].
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's have some 21st‑century programming We have been using Java8 for 21 months now. Java7 is beyond its end of life and it is now nearer to Java9 than it was to the release of Java8.A Stream is an object which can iterate things without your having to write for do or while, and there is a specialised form (=subtype) called IntStream which iterates ints. That has two methods called rangeXXX and this rangeXXX method probably does what you want. It take the numbers given as arguments and produces a Stream containing the appropriate range of numbers in order. Then you can simply use the toArray method method to get an array.

You would usually indent the code like this:-
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Knowski wrote:. . . Have a look at some array examples to make sure that number = i is the correct way to set a value. . . .

It isn't. That code won't compile.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Molayo Decker wrote:I'm not trying to create another array that can hold 500 primitive integers. I'm trying to find a way to create an array that "CONTAINS" "NOT HOLD" primitive integers from 1 - 1000.


There's no point in getting shirty. Sam's trying to help you...and so is Campbell.

The fact is that your code (the one that Sam repeated for you) WON'T COMPILE. So what error are you getting?

You are VERY close to a solution; you're just coding it wrong.

Winston
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Knowski wrote:. . . Have a look at some array examples to make sure that number = i is the correct way to set a value. . . .

Somebody pointed out that I have probably misunderstood you there. Sorry.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:We have been using Java8 for 21 months now.
(...)


We may have, but OP?
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. The java.util.stream worked perfectly. Sorry if i sounded hash with my reply. It really wasn't trying to. Campbell the assignment is giving me an java.util.IllegalFormatConversionException error.
Do you please have a solution to this. I wrapped it in a try/catch clause but did not work.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Molayo Decker wrote:Do you please have a solution to this. I wrapped it in a try/catch clause but did not work.


I don't have a solution for that, but I'm going to do something I VERY rarely do - give you an implementation for an integer range
- and the reason is that it doesn't involve any arrays at all.

Viz::I've deliberately left the documentation sparse so you can work out what it does for yourself.

And the reason I've done it is to prove an important point - just because you think a class should be implemented a certain way (probably because it's what you're used to) doesn't mean it has to be. I think the class above does most things we want to do with a range of values - and if not, you can always make it better by adding to it.

But most importantly, I can define your range of 0-1000 with
  IntRange zeroToOneThousand = new IntRange(0, 1001);
and it'll take up 40 bytes (or so) of memory instead of 4,004.

HIH

Winston
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Wilson.
I ended up using the below and it worked. I will run both to check memory usage.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Molayo Decker wrote:Thanks Wilson. I ended up using the below and it worked...


We're not in the habit of publishing code that doesn't ; but you''re quite right to test it.

It would seem, however, that you haven't read thoroughly enough, because what you wrote will NOT return a range that includes both 0 and 1000.

I suggest you look over it again. Carefully. My reason for writing it was so that you could learn from it; not parrot it.

Winston
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I know I'm coming late into the piece, but you do know to use square brackets with arrays right?
Here is an example involving Strings:
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston,
Can i get reference documentation on how to do this r.start and r.end. Parameters referencing primitive variables? Am new to this. I don't really get the logic. I know parameters can reference methods but not primitive.
Please help me out here. It will help me understand the code. Thanks.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Molayo Decker wrote:Hi Winston,
Can i get reference documentation on how to do this r.start and r.end. Parameters referencing primitive variables? Am new to this. I don't really get the logic. I know parameters can reference methods but not primitive.
Please help me out here. It will help me understand the code. Thanks.


Oh, OK. Don't worry too much about it then.

I can't do anything now, but check back tomorrow and I'll try and add a bit of explanation; but I don't want to swamp you with too much stuff too soon (maybe I have already).

Winston
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Molayo Decker wrote:. . . java.util.IllegalFormatConversionException error.. . . did not work.

Have been too busy to answer earlier.

Did you look up that Exception to see when it occurs? Did you read the stack trace to see where it occurred? Didn't the message in the stack trace give you any help to explain it?
 
reply
    Bookmark Topic Watch Topic
  • New Topic