CodeGym /Courses /Java Syntax /Basic actions with arrays

Basic actions with arrays

Java Syntax
Level 7 , Lesson 3
Available

"Here are a few examples of interesting things you can do with arrays:"

Example 1.
Fill a 10-element array with numbers from 1 to 10:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = i + 1;
        }
    }
}
Fill a 10-element array with numbers from 10 to 1:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = 10 - i;
        }
    }
}
Fill a 10-element array with numbers from 0 to 9:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = i;
        }
    }
}
Fill a 10-element array with numbers from 9 to 0:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = 9 - i;
        }
    }
}
Example 2.
Read 10 strings from the keyboard:
public class MainClass
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String[] list = new String[10];

    for (int i = 0; i < list.length; i++)
    {
      list[i] = reader.readLine();
     }
  }
}
Read 10 numbers from the keyboard:
public class MainClass
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] list = new int[10];

    for (int i = 0; i < list.length; i++)
    {
      String s = reader.readLine();
      list[i] = Integer.parseInt(s);
    }
  }
}
Example 3.
Display an array on the screen:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        int[] list = new int[10];

        // Fill the array
        for (int i = 0; i < list.length; i++) 
           list[i] = i;

        // Display the contents of the array
        for (int i = 0; i < list.length; i++)  
          System.out.println(list[i]);
    }
}
Example 4.
Quick (static) initialization. Add up the array elements:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        // Static initialization
        int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};        
        
        // Calculate the sum
        int sum = 0;
        for (int i = 0; i < list.length; i++) 
           sum += list[i];

        System.out.println("Sum is " + sum);
    }
}
Example 5.
Find the smallest element in an array:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};

        int min = list[0];
        for (int i = 1; i < list.length; i++)
        {
             if (list[i] < min) 
                  min = list[i];
        }

       System.out.println ("Min is " + min);
    }
}

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


7
Task
New Java Syntax, level 7, lesson 3
Locked
Welcome! But not everyone.
Welcome! But not everyone.
7
Task
New Java Syntax, level 7, lesson 3
Locked
A cube calculator
A cube calculator
7
Task
New Java Syntax, level 7, lesson 3
Locked
A cubed cube calculator
A cubed cube calculator
Comments (41)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Benjamin Joseph Lentine Level 8, Songwon, South Korea
16 July 2024
System.out.println(words[(words.length - 1) - i]; Yeah, I might need to work on not overcomplicating things.
Mindaugas Level 23, Lithuania
22 August 2022
just do not add your own text before or after reader, because there will be more than 10 strings on screen and test will fail :)
Galax Level 10, Houston, United States
21 February 2022
Everyone, keep up the good work! You can do it! You are impossible to defeat!
Aleksa Level 18, Belgrade, Serbia
20 March 2022
what?
Galax Level 10, Houston, United States
20 March 2022
what do u mean?
Walter Level 7, Flint, michigan, United States
31 March 2022
اللعنة على عائلتك ، الموت لأهل بيتك ، أنا واحد مع الكل والموت نفسه
Gort Level 7, Gortville, United States
31 March 2022
Brentachii Level 13, United States of America, United States
8 December 2021
all of those examples {New Java Syntax Level 6, Lesson 3} should be made into tasks that can be done.
Lance Wilson Level 7, United States of America, USA
28 March 2022
don't need redundant tasks. if you can implement one of them, you should be able to implement all
Hoist Level 12, San Diego, United States
9 October 2021
Hint: what does NULL do when it appears that only 8 element slots are taken in a 10 space Array ? Hint2: build from the examples about and then step thru the output possibilities
Angelo Tratsis Level 8, Boston, United States
4 May 2021
if you got this far is obvious that you are capable to do anything you put in your mind. Is not easy most of the times but in case to grow we need to go through discomfort. Keep it up you doing amazing job.. You are unstoppable ..
MCantu Level 2, Texas, United States
24 December 2020
I am a visual learner therefore I love these examples...very practical indeed!
Mihai Bone Level 8, Bucharest, Romania
15 October 2020
I notice a lot of people being lvl 7, so here comes the stuck point ?
Attila Level 7, Hainault, United Kingdom
7 September 2020
I would like to ask when is cameing that level when we create a matrix , I mean for in for loop ?
Agent Smith Level 38
14 August 2020
Example 3, line 15 - a typo there.