9

Im still starting out in java - and any guidance would be great on this. I'm basically hoping to create an array, then assign values to that array in a for loop. The code I have at the moment is:

int i;
int[] testarray = new int[50];

for (i = 0; i <=50; i++) {  
testarray[i]=i;
}

All i wanna do is make an array with each entry the number of the iteration (using this method) I know its really simple, but I feel as if I have missed something important during learning the basics! Thanks!

1
  • What's wrong with your current code? It looks fine to me. Commented Jul 15, 2012 at 20:01

5 Answers 5

18

Everything is fine except the stop condition:

for (i = 0; i < 50; i++) {  

Since your array is of size 50, and indices start at 0, the last index is 49.

You should reduce the scope of i, avoid hard-coding the length everywhere (don't repeat yourself principle), and respect the camelCase naming conventions:

int[] testArray = new int[50];

for (int i = 0; i < testArray.length; i++) {  
    testArray[i]=i;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Read the error messages and try to understand what they mean, instead of just treating them as an error flag. The error message probably said: ArrayOutOfBoudsException, and probably told you the index you tried to access, but was out of bounds.
6

Your array has 50 elements, and your loop goes over 51 elements (0 to 50).

Just change the code to:

int[] testarray = new int[50];

for (int i = 0; i < 50; i++) {  
    testarray[i] = i;
}

or better:

int[] testarray = new int[50];

for (int i = 0; i < testarray.length; i++) {  
    testarray[i] = i;
}

1 Comment

Also, you can move the declaration of i into the for loop, rather than outside of the loop.
0

Use length of the array instead of hard-coding 50.

for (i = 0; i <testarray.length; i++) 

Comments

0
    //Create a function that takes two numbers as arguments (num, length) and returns
    // an array of multiples of num until the array length reaches length.
    package ArrayPractice;

    public class Question1 {

        //Creating array method
        public int[] arrayofMultiples(int number, int demo) {
            int[] testing = new int[demo];
            int y = 1;
            int z = number;
            for (int i = 0; i < testing.length; i++) {
                number = z * y;
                testing[i] = number;
                y++;
            }
            return testing;
        }

        public static void main(String[] args) {
            Question1 question1 = new Question1();
            // Creating an array to get the get values of array method
            int[] t1 = question1.arrayofMultiples(17, 6);
            for (int i : t1) {
                System.out.println(i);
            }
        }
    }

Comments

0

Program to input elements in an array in java using for loop

import java.util.*;

public class Main{

    public static void main(String[]args){

        Scanner a=new Scanner(System.in);

// code starts here

        int length;
        length=a.nextInt();    
        int [] arr = new int[length];    
        for(int i=0;i<length;i++){
            arr[i]=a.nextInt();    
        }
        
        for(int i=0;i<length;i++){   
            System.out.println(arr[i]);   
        }
    }
}

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.