BP Forums
Integer Arrays! - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+---- Thread: Integer Arrays! (/showthread.php?tid=405)



Integer Arrays! - brandonio21 - 10-02-2011

I recently made a video tutorial on integer arrays, and some people may just want to copy/paste the code for their own purposes, so here it is, fully commented!
[code2=java]public class arrayTutorial { //Create the class

public static void main(String[] args){ //Create the main method (This is what gets executed)

int arrInt[] = new int[5]; //Create a new integer array with 5 slots
//Note: Indices start from 0
arrInt[0] = 1; //Tell the first slot (index) in the array to have a value of 1

int i = 1; //Give i a value of 1
while (i <= 4){ //If i is less than 5
arrInt[i] = i+1; //Assign the corresponding slot of the array (i) a value of 1 + i; so if i is 2, the 3rd slot of the array would be filled with 2+1, which is 3.
i++; //Increase the value of i by one

}
System.out.println(arrInt[0]+ " " + arrInt[1] + " " + arrInt[2]+ " " + arrInt[3] + " " + arrInt[4]); //Output all the slots in the array in string form (seperated by spaces)


}

}[/code2]

Basically, what I am doing here, is creating a new integer array with 5 slots, using a while statement to fill the 5 slots, and displaying each slot, separated by a space.

Hopefully this helps anyone actually doing Java (As I know this forum is mostly vb.net based)


Re: Integer Arrays! - WitherSlayer - 01-03-2013

Can you also do String arrays? Even char arrays?


Re: Integer Arrays! - brandonio21 - 01-03-2013

WitherSlayer Wrote:Can you also do String arrays? Even char arrays?
Indeed! Arrays can hold any object whether it be int, String, long, char, or even a custom object.