Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filling an array with a value

Status
Not open for further replies.

tictaclam

Programmer
Jan 8, 2004
47
US
I don't think there is a way to do this but i just wanted to check. Is there any way to fill an array with a value say the number 2 without looping through it?
thanks
 
I'm going mad.

int[] arr = new arr[5];

should be :

int[] arr = new int[5];


--------------------------------------------------
Free Database Connection Pooling Software
 
i guess i wasn't clear i want to set every value in the array to 2. Like

int[] arr = new arr[5];
arr[0] = 2;
arr[1] = 2;
...
arr[4] =2;

obviously i could use a loop here instead...but is there any shortcut to doing this besides a loop?
 
well, thats why you would use a loop !

If you have an array to copy, you can use System.arraycopy(), but apart from that - use a loop !

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks, that's what i figured i just wanted to double check and make sure there wasn't a way to create the array filled with a dummy value, (since you can do it other languages). thanks though i'll just loop through
 
out of interest - in what programming languages can you do that without explicitly looping/assigning ?

--------------------------------------------------
Free Database Connection Pooling Software
 
hmm i really can't think of it off the top of my head...i know i have done it before in something. At work i'm using a different language every few weeks so it's hard to remember which one it was, sorry. If i think of it i'll post it.
 
Forgot about that one ...

Still just uses a loop though - its only a convenience method !

The Sun src code :

Code:
    /**
     * Assigns the specified Object reference to each element of the specified 
     * range of the specified array of Objects.  The range to be filled
     * extends from index <tt>fromIndex</tt>, inclusive, to index
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the 
     * range to be filled is empty.)
     *
     * @param a the array to be filled.
     * @param fromIndex the index of the first element (inclusive) to be
     *        filled with the specified value.
     * @param toIndex the index of the last element (exclusive) to be
     *        filled with the specified value.
     * @param val the value to be stored in all elements of the array.
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
     *	       <tt>toIndex &gt; a.length</tt>
     */
    public static void fill(Object[] a, int fromIndex, int toIndex,Object val){
        rangeCheck(a.length, fromIndex, toIndex);
        for (int i=fromIndex; i<toIndex; i++)
            a[i] = val;
    }

--------------------------------------------------
Free Database Connection Pooling Software
 
Without browsing the source, I would comment: nearly!
Code:
public static void fill (Object[] a, int fromIndex, int toIndex, int val)
{
        rangeCheck(a.length, fromIndex, toIndex);
        for (int i=fromIndex; i<toIndex; i++)
            a[i] = val;
}

and yes - only convenience - but in the end, it's all only convenience - only convenience not to write machine-instructions directly :)

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top