0

I am a new java lerner. I have a problem to understand how can I access in a for loop in Array.

public class ArrVers {
public static void main (String args []) {

}

public static int Annahme (int [] a) {

    int platzhalter [] = {1,3,4,5,6};

    for(int i = 0; i<platzhalter.length; i++) {

        return platzhalter [i+2];

    }
    return 0;

    }

}

I want to return every second Element of this array. Example {0,2,4,6,8} Here i want to print 0,4,8 on screen with "[".

Sorry for my bad English! This is my code so far. I know im a noob yet, but i want to improve my skills and learn more.

2
  • that is not every second element . I feel that it's you want to print a value which is on even index? Commented Dec 29, 2019 at 14:19
  • Yes, i also want to return the index. Can i edit my for loop with i+2? Commented Dec 29, 2019 at 14:24

1 Answer 1

1
  public static void main(String[] args) 
    { 
        int platzhalter [] = {0,2,4,6,8};
        for (int i=0;i<platzhalter.length;i=i+2){
            System.out.print(platzhalter[i]);
        }
    } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.