Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
Java in General
Allocates number within of array.
Gustavo Siqueira
Ranch Hand
Posts: 97
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello the all!
I want allocates number within of one array, see the program:
import java.util.Scanner; public class Core { int[] class1; //declares an array of integers int[] class2; //declares other array of integers int select; //get data of user //creates Scanner for get dados of user Scanner input = new Scanner(System.in); public Core() { class1 = new int[5]; //declares array for 5 class class2 = new int[5]; //declares array for 5 class System.out.println("Please type 1 for First Classe :"); select = input.nextInt(); if(select == 1) { class1[0] = 1; } System.out.println("Class1: "); int i = 0; while(i <= class1.length) { System.out.println(class1[0]); i++; } } }
See the result:
Class1:
1
1
1
1
1
1
Or is, this allocating in all memory of array.
How to solved the problem ?
Rob Spoor
Sheriff
Posts: 22783
131
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You're printing the first element (
class1[0]
) each time.
Also, your loop condition should be
while(i < class1.length)
; so
<
and not
<=
. Arrays in
Java
start at 0 and go to the length - 1 inclusive, or the length exclusive.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Array problem
and yet another recursive problem
Wanted some help about arrays
Accessing component from main class.... Help!
something more about arrays
More...