Results 1 to 7 of 7
Thread: Range within an Array
- 04-17-2009, 06:29 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Printing a range of elements from an Array
Sorry for the vague title, could not think of a better fitting one.
I have an Array and I would like to print a range of index's from this Array, so for example:
My Array could contain the letter A in index [0], the letter B in index [1] and so on. Is there anyway for me to print the letters A B C D contained in an Array of the whole alphabet, onto one line. So in other words print the first 4 letter of the alphabet.
System.out.println(alphabet[0-3]);
This of course only minuses 3 from 0.
Thanks for reading :), any tips or pointer's in the right direction appreciated.Last edited by End; 04-17-2009 at 06:34 PM.
- 04-17-2009, 06:43 PM #2
You could use a for loop
Just change the 3 to whatever the limit you want to iterate to is. You can also change i to start out at 1 if you want to print the array [1-3].Java Code:for(int i = 0; i <=3; i++) { System.out.println( myArray[i] ); }
Mr.Beans
- 04-17-2009, 06:56 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Hey, thanks for the fast reply :). I dont know if I am doing it wrong but this prints:
A
B
C
D
I would like to try and print:
A B C D
Sorry for not explaining this clearly :o
- 04-17-2009, 06:59 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
instead of using println, just use print.
- 04-17-2009, 07:01 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Oh wow! I can not believe it was that simple. Thanks very much :D.
- 04-18-2009, 09:00 AM #6
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
I dont think you should have given the source code. There will be no learning if we provide the source code.
Tshegofatso Manakana
a.k.a Untouchable ™
- 04-18-2009, 06:53 PM #7
Tshegofatsom... If your refereing to the snippet of code (it's not a whole program) that Mr.Bean posted, I don't think there to much problem with that little piece of code. I do agree with you that complete solutions/programs should not be posted, because there's is no learning process involved (cut & paste & forget).
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
[SOLVED] Expected Range: Red Green Blue
By AndrewM16921 in forum New To JavaReplies: 1Last Post: 04-03-2009, 07:59 PM -
Writing integer pixel array(Range:0-255) into .txt file
By Mazharul in forum Java 2DReplies: 3Last Post: 08-24-2008, 01:51 PM -
What is the best way to represent range of number...
By zoe in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:13 AM -
Can I set a range in case statement?
By christina in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks