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: @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 (with line numbers):

15 int limit = Integer.parseInt(args[0]); 
16 double[] numbers = new double[limit]; 
17 int j = 0; 
18 for (int i = 2; i < limit; i++) { 
19     numbers[j] = i; 
20     j++; 
21 }

Is there a way to make this a little more eloquent?

Also, when I run this program - what code I've listed compiles without a complaint - I get the following error:

Exception in thread "main" java.lang.NullPointerException at Program.main(Program.java:19)

What gives?

Recommended Answers

All 5 Replies

Thats because you're making an array of 0 length. There won't be a value at position 2. (Which is really item number 3 because I think arrays start from value 0.)

...I'm sorry hooknc, I don't see how I'm making an array of zero length - the size of the array is set by limit which is specified from the users first argument.

And, yes, array elements do start at zero (0), but I'm taking care of that with the variable, j, and using variable, i, to assign the values to the array.

You are correct. I miss read the following code:

int limit = Integer.parseInt(args[0]);

as...

int limit = Integer.parseInt(0);

I actually copied your code and ran it. It seemed to run fine. What is your input args[0] value?

I'm just using a small value of 10.

Pretty much, what I'm writing (as to help me learn Java) is a Sieve of Eratosthenes (http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes), it's a prime number finder.

Here's my code currently:

07 import static java.lang.System.out;
08 import java.util.*;
09
10 public class Eratosthenes {
11    public static ArrayList<Integer> primes = new ArrayList<Integer>();
12    public static void main(String[] args) {
13       int limit = Integer.parseInt(arg[0]);
14       int start = 2;
15       int[] numbers = new int[limit];
16
17       for (int i = 0; i <= limit; i++) {
18          if ((i + start) > limit) break;
19          numbers[i] = i + start;
20       }
21
22       /* This is the part that's generating faulty output */
23       for (int n : numbers) {
24          for (int x = 1; x < n; x++) {
25             if (n % x != 0) primes.add(n);
26          }
27       }
28 
29       primes.trimToSize();
30
31       for (int p = 0; p <= primes.size(); p++) out.print(primes.get(p) + "\t");
32 
33       out.println();
34    }
35 }

This code might complain about being dangerous when compiling, so try compiling it with the -Xlint argument: $ javac -Xlint Eratosthenes.java .

I'm no longer getting an exception that prohibits the runtime of the class, but the math behind the output needs refinement (because as it is, it's printing out every number between start and limit .

I was just getting too bogged down trying to fix the old code, so I decided to start fresh, and guess what - I got it!

// Eratosthenes Prime Number Finder Algorithm
/**
* @author Nick Saika
* @version 1.1
*/
import java.util.*;
public class Eratosthenes {
   public static ArrayList<Integer> numbers;
   public static ArrayList<Integer> primes = new ArrayList<Integer>();
   public static final int start = 2;
   public static int limit;
   public static void main(String[] args) {
      limit = Integer.parseInt(args[0]);
      numbers = new ArrayList<Integer>(limit);
      for (int i = 0; i < limit; i++) {
         if ((i = start) > limit) break;
         numbers.add(i, i + start);
      }
      for (int i = 0; i < numbers.size(); i++) {
         if (number.get(i) == 0) continue;
         primes.add(numbers.get(i));
         clearMultiplesOf(numbers.get(i));
      }
      for (int i = 0; i < primes.size(); i++) System.out.print(primes.get(i) + " ");
      System.out.println();
   }
   public static void clearMultiplesOf(int n) {
      for (int m = 2; m < (limit / 2); m++) {
         int multiple = n * m;
         if (multiple == 0) continue;
         for (int i = 0; i < numbers.size(); i++) {
            if (numbers.get(i) == multiple) numbers.set(i, 0);
         }
      }
   }
}

:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.