| | |
twenty integers in an array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 7
Reputation:
Solved Threads: 0
Is this a basic Java I prodject? you should be able to look up arrays in your books index. There are a couple different ways to declare an array, one of which is;
int [] Intarr = new int[20];
int is the data type in this array, new int, creates the array, 20 is the number of positions. As for sequentially accessing it, do you know what a for loop is?
int [] Intarr = new int[20];
int is the data type in this array, new int, creates the array, 20 is the number of positions. As for sequentially accessing it, do you know what a for loop is?
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Something like this?
Java Syntax (Toggle Plain Text)
class ArrayOfInts { public static void main(String[] args) { int[] myNums = new int[20]; for (int x=0; x<20; x++) { myNums[x] = x * 5; } for (int y=0; y<myNums.length; y++) { System.out.println("myNums[" + y + "] = " + myNums[y]); } } }
•
•
Join Date: Sep 2004
Posts: 35
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by shantuli
hi everyone,
can anyone solve me the code for the java problem.
i need a java programthat holds twenty integers in an array, loop though each cell sequentially and prints each number on the console.
thank you very much.
shantuli
tht's very easy
class Test{
public static void main(String args[]){
int[] arr = new int[20];
for(int j=0;j<20;j++){
arr[j]=j;
}
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
arr is array containing 20 integers
this will print first 20 from 0 to 19
this will do the job
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
Originally Posted by tigerxx
tht's very easy
class Test{
public static void main(String args[]){
int[] arr = new int[20];
for(int j=0;j<20;j++){
arr[j]=j;
}
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
arr is array containing 20 integers
this will print first 20 from 0 to 19
this will do the job
![]() |
Similar Threads
- Code Snippet: Program to find the largest sum of contiguous integers in the array. O(n) (C)
- I have to write a program to read 10 integers into an array (C++)
- help to sum integers in an array (Java)
Other Threads in the Java Forum
- Previous Thread: Documents
- Next Thread: Need some help with my do-while and while loops
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






