LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-22-2006, 06:17 PM   #1
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 64
Java - Assigning Array Values en masse


Is there a way in Java to assign values en masse to an array?

To give an idea of what I'm trying to achieve, in Perl, you can do:
Code:
@listofvalues = (1..$anynumber);
to add (in this case) all digits between 1 and $anynumber to the array @listofvalues.

Is there a Java equivalent, or another way of doing this in Java (with that kind of ease)?

So far, I have:
Code:
int limit = Integer.parseInt(args[0]);
double[] list = new double[limit];
int j = 0;
for (int i = 2; i < limit; i++) {
     list[j] = i;
     j++;
}
Is there a way to make this a little more eloquent?
 
Old 08-22-2006, 07:58 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352

Rep: Reputation: 129Reputation: 129
As far as I am aware you need to use a loop in Java, but I think that your code will miss the last two elements of the array, you could try the following

Code:
int limit = Integer.parseInt(args[0]);
int start = Integer.parseInt(args[1]);
double[] list = new double[limit];

for (int i = 0; i < limit; i++) {
     list[i] = i+start;
}
 
Old 08-23-2006, 09:46 AM   #3
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Original Poster
Rep: Reputation: 64
Thanks for the reply graemef, however I don't need the user to specify a starting point - the starting value is fixed at 2 (which is assigned to list[0]).

How will it miss the last two array elements?

Last edited by indienick; 08-23-2006 at 10:06 AM.
 
Old 08-23-2006, 10:11 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> How will it miss the last two array elements?

in your original code j starts out 2 behind i. when i reaches limit (which is also the size of the array) j is still 2 behind i. loop stops and the last 2 in array are left unset.
 
Old 08-23-2006, 10:25 AM   #5
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Original Poster
Rep: Reputation: 64
OOOoooooooooooo alright. thanks xhi.
 
Old 08-23-2006, 10:33 AM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Original Poster
Rep: Reputation: 64
Here's the new code (with line numbers):
Code:
15 int limit = Integer.parseInt(args[0]);
16 int start = 2;
17 
18 int numbers[] = new int[limit];
19
20 for (int i = 0; i < limit; i++) numbers[i] = i + start;
The code compiles without a complaint, however I get an error when I try to run it:
Code:
Exception in thread "main" java.lang.NullPointerException
        at Eratosthenes.main(Eratosthenes.java:20)
What gives?

EDIT: Also, is there a way to create arrays with unspecified (because they're unknown) lengths? (ie. Perl reference again - lists/arrays just exist with variable lengths).

Last edited by indienick; 08-23-2006 at 11:19 AM.
 
Old 08-23-2006, 11:01 AM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> Also, is there a way to create arrays with unspecified (because they're unknown) lengths? (ie. Perl reference again - lists/arrays just exist with variable lengths).

yes, use an ArrayList or other container.

Code:
18 int numbers[] = new int[limit];
19
20 for (int i = 0; i < limit; i++) list[i] = i + start;
you are creating an array named numbers then using array called list.. is that what you intended?
 
Old 08-23-2006, 11:18 AM   #8
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Original Poster
Rep: Reputation: 64
Oops, sorry about that - I'm creating and using a list called numbers.
 
Old 08-23-2006, 11:22 AM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
is that what was causing the exception?
 
Old 08-23-2006, 11:25 AM   #10
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Original Poster
Rep: Reputation: 64
no no...I just overlapped an old code with new code.

And thanks for the ArrayList hint.
 
Old 08-23-2006, 11:30 AM   #11
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
im not understanding you completely. are you still getting the exception? if you are the only thing that i can see that would be causing it is the args that the program is receiving.

if you are not getting the exception, well good
 
Old 08-23-2006, 12:28 PM   #12
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Original Poster
Rep: Reputation: 64
I'm not getting the exception any more, xhi.

Thanks for all your help (you too, graemef).

All I have to do now is fix up the math that calculates which of the numbers from start to limit are prime.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting values from an array bahadur Programming 2 08-14-2006 01:07 AM
help with shell script - fill an array with values from a file delmoras Linux - General 1 07-17-2006 11:19 AM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM


All times are GMT -5. The time now is 04:41 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration