Java Arrays

Java Arrays

Greetings friends! Today we will begin our introduction to some infamous or famous, depending on who you ask lol, data structures used in development work. Our topic today will be Java Arrays! Arrays are a type of data structure that allows you to 'store' elements(items, strings, integers) in indexes(spots, slots, positions) and recall them when you need to use them. Not to be confused with it's cousin, the ArrayList, Arrays are limited by the initialized size(amount of slots you desire) and mostly are accessed using the for or for each loop. Today, we will use the for loop to access our items within the array. Let's dive in!

Array example in Java and IntelliJ

We first begin with number one. Typically we declare arrays with a data type of String, followed by brackets[](this declares an Array) and the name of our array 'toysInBox'. After the equals sign, we use curly braces {} with string names in parentheses and separated by commas. Within our toysInBox array, we now have five elements that hold a position in what we call indexes. The very first string, "Buzz Lightyear" is in the 0 index and our "Tonka Truck" is in the number 4 index. In arrays, unlike how we normally begin counting with 1, you want to remember the index at 0 is the first element or index in the array.

Next, number two is the for loop we are using to go through (traverse, search, travel through) our array to find the elements within. As in my previous article on for loops(please check them out for a more comprehensive explanation), we have the initialized variable i equal to zero, the test condition that states 'as long as i is less than the length of the number of indexes(elements) in the array then the condition is true until false, and finally, our updation to count each loop occurrence until our test condition is false.

For number three, we want to print out our results of the loop by looking at the index(i) of each element in our array.

Finally, for number four, we see our expected result.

I hope that was fun for you as much as it was for me! Arrays can be a fun way of storing, retrieving, and searching for data and are commonly used in various programming languages. I want to encourage you to try them out and see what you can do. As always, stay Java and take care until next time.


要查看或添加评论,请登录

David K.的更多文章

  • What a year(2023 Recap)!!

    What a year(2023 Recap)!!

    Greetings friends! I know, I know, it has been some time since we've heard from one another, but let me explain a bit…

  • Java - For and ForEach Loops

    Java - For and ForEach Loops

    Good day everyone! Today's topic, while we are sadly coming to the end of the conditional statements, will revolve…

    4 条评论
  • Java - While statement

    Java - While statement

    Greetings Java fam!!! In this edition of the New Dev Journey, we will touch on the Java while statement. The great…

  • Java's Do-While statement

    Java's Do-While statement

    Good morning everyone! Today's topic will be another one of Java's conditional statements, the do-while statement. The…

    4 条评论
  • Java...If else statement

    Java...If else statement

    Java if-else example Greetings! Today's article we will dive into Java statements, in particular, the if-else…

    1 条评论
  • Java Primitive Data Types???

    Java Primitive Data Types???

    Hello everyone! Today's example will be Java's eight(8) primitive data types. These data types are very important to…

  • Java Class Example

    Java Class Example

    So, today, we are continuing our journey of learning Java. I wanted to show a small example of a Java class, attributes…

    2 条评论
  • What does a Java Program look like???

    What does a Java Program look like???

    Greetings everyone! I have posted a small snippet of what a Java program looks like. If you have read any of the…

  • Java - Objective-Oriented Programming principles

    Java - Objective-Oriented Programming principles

    So, the last time we left off, we were introduced to writing our first piece of Java code and what that looks like…

  • Java - where do I start?

    Java - where do I start?

    So, last week was a brief introduction into Java and I do mean brief. Now, I want to dive into and answer the question,…

社区洞察

其他会员也浏览了