Jump to content
yoyo

Java Question

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

  • Like 3

Share this post


Link to post
Share on other sites

 for(j=0;j<anArray.length;j++)
{
System.out.println(anArray[j]);
}
}
}

 

this worked thanks.

Share this post


Link to post
Share on other sites

Try this

 

 

anArray...
j...

for(j = 0; i < anArray.length; j++) {
    System.out.println(anArray[j]);
}

 

/JoNny

Share this post


Link to post
Share on other sites

i recommand working with java8 tho instead of java7. its much easier to use and its alot less code to write.

Share this post


Link to post
Share on other sites

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).

  • Like 1

Share this post


Link to post
Share on other sites

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

  • Like 1

Share this post


Link to post
Share on other sites

 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.

  • Like 1

Share this post


Link to post
Share on other sites
        .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

  • Like 1

Share this post


Link to post
Share on other sites


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 by DrJoske

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

About Us

We are glad you decided to stop by our website and servers. At Fearless Assassins Gaming Community (=F|A=) we strive to bring you the best gaming experience possible. With helpful admins, custom maps and good server regulars your gaming experience should be grand! We love to have fun by playing online games especially W:ET, Call of Duty Series, Counter Strike: Series, Minecraft, Insurgency, DOI, TF2 & Battlefield Series and if you like to do same then join us! Here, you can make worldwide friends while enjoying the game. Anyone from any race and country speaking any language can join our Discord and gaming servers. We have clan members from US, Canada, Europe, Sri Lanka, India, Japan, Australia, Brazil, UK, Austria, Poland, Finland and many other countries. It doesn't matter how much good you are in the game or how much good English you speak. We believe in making new friends from all over the world. If you want to have fun and want to make new friends join up our gaming servers and our VoIP servers any day and at any time. .

×

Important Information

By using this site, you agree to our Terms of Use.