Jump to content

Java Question


tipsy

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.

Link to comment
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

Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

  • 2 weeks later...

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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