tipsy Posted February 7, 2016 Posted February 7, 2016 What I want is to print values in array index 10, 20,30,40 ... likewise using for loop. class coolExample{public static void main(String[] args){int anArray[];int j;anArray= new int[10];anArray[0]=10;anArray[1]=20;anArray[2]=30;anArray[3]=40;anArray[4]=50;anArray[5]=60;anArray[6]=70;anArray[7]=80;anArray[8]=90;anArray[9]=100;for(j=0;j<anArray.length;j++){System.out.println(anArray);}}} Output I'm getting from running this is : 0,1,2,3,4....9 and I tried this for(j=0;j<anArray.length;j++){anArray[j]=j;System.out.println(anArray[j]); Same result. Quote
Bowly Posted February 7, 2016 Posted February 7, 2016 anArray.forEach(System.out.println()); i think its something like this. lemme know if it doesn't work. btw this is java8 im using Edit: i just remembered it would be like this: anArray.forEach(System::println); or anArray.forEach(a -> System.out.println(a)); i think both are correct Quote
Bowly Posted February 7, 2016 Posted February 7, 2016 for(j=0;j<anArray.length;j++) { System.out.println(anArray); } } } Output I'm getting from running this is : 0,1,2,3,4....9 and I tried this for(j=0;j<anArray.length;j++) { anArray[j]=j; System.out.println(anArray[j]); Same result. In your first loop you're not printing the values and in your second you're overwriting the value thats inside your array you must leave out anArray[j]=j and it should work 3 Quote
tipsy Posted February 7, 2016 Author Posted February 7, 2016 for(j=0;j<anArray.length;j++){System.out.println(anArray[j]);}}} this worked thanks. Quote
JoNny Posted February 7, 2016 Posted February 7, 2016 Try this anArray...j...for(j = 0; i < anArray.length; j++) { System.out.println(anArray[j]);} /JoNny Quote
Bowly Posted February 7, 2016 Posted February 7, 2016 i recommand working with java8 tho instead of java7. its much easier to use and its alot less code to write. Quote
Pepperonipizza Posted February 16, 2016 Posted February 16, 2016 There is a bunch of stuff you could be doing more elegantly/efficiently. I re-wrote your program the way I would write it: public static void main(String []args){ int anArray[] = new int[10]; // Construct array for (int i = 0; i<10; i++) { anArray[i] = 10*i; } // Print array for (int el : anArray) { System.out.println(el); } } Pay special attention to the for (int el : anArray) line. This is Java's "smart for-loops" feature that is very useful. El will iterate over all values in anArray, regardless of what type of a container that anArray is. I usually program in C++ and this feature is invaluable there (since iterators can be pretty indirect to work with at times when you don't really need them). 1 Quote
JoNny Posted February 16, 2016 Posted February 16, 2016 There is a bunch of stuff you could be doing more elegantly/efficiently. I re-wrote your program the way I would write it: public static void main(String []args){ int anArray[] = new int[10]; // Construct array for (int i = 0; i<10; i++) { anArray[i] = 10*i; } // Print array for (int el : anArray) { System.out.println(el); } } Pay special attention to the for (int el : anArray) line. This is Java's "smart for-loops" feature that is very useful. El will iterate over all values in anArray, regardless of what type of a container that anArray is. I usually program in C++ and this feature is invaluable there (since iterators can be pretty indirect to work with at times when you don't really need them). And even that is no good. This takes way too much of load time. Two for / -each loops is still one too much. As you know what you will insert (otherwise you wouldn't use an array), you can directly insert the values upon variable initalization. Like so: public static void main(String[] args){ int anArray[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; // Print array for (int el : anArray) { System.out.println(el); } } /JoNny 1 Quote
Pepperonipizza Posted February 16, 2016 Posted February 16, 2016 As you know what you will insert (otherwise you wouldn't use an array), you can directly insert the values upon variable initalization. True. However, if there are such strict performance concerns, Java isn't the language one should be looking at. You will need something more along the lines of C++ in such a case. 1 Quote
parrot Posted February 16, 2016 Posted February 16, 2016 .data myArr: .space 40 .text la $t0, myArr li $t1, 20 sw $t1, 4($t0) li $t1, 30 sw $t1, 8($t0) li $t1, 40 sw $t1, 12($t0) li $t1, 50 sw $t1, 16($t0) li $t1, 60 sw $t1, 20($t0) li $t1, 70 sw $t1, 24($t0) li $t1, 80 sw $t1, 28($t0) li $t1, 90 sw $t1, 32($t0) li $t1, 100 sw $t1, 36($t0) li $t1, 10 sw $t1, ($t0) li $t2, 1 li $v0, 1 j condition loop: sll $t4, $t2, 2 add $t3, $t0, $t4 lw $a0, ($t3) syscall addi $a0, $0, 0xA addi $v0, $0, 0xB syscall addi $v0, $0, 1 addi $t2, 1 condition: bne $t1, $t2, loop li $v0, 10 syscall My assembly is a bit rusty, but I think that works 1 Quote
Jefke Posted February 17, 2016 Posted February 17, 2016 (edited) public static void main(String[] args) { Arrays.stream(new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }).forEach(System.out::println); } //Going to far with it public static void main(String[] args) { Stream.of(10, 20, 30, 40, 50, 60, 70, 80, 90, 100).forEach(System.out::println); } public static void main(String[] args) { int anArray[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; System.out.println(Arrays.toString(anArray)); } public static void main(String[] args) { System.out.println(Arrays.toString((new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }))); } /* Now I'm going to stop, I went over board with it * Also doesn't follow the task anyway */ Edited February 17, 2016 by DrJoske Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.