• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Lists in Java

February 18, 2022 Java

In this post, you will learn how to work with lists so that you can use that same knowledge and thus evolve.

  • a list is an ordered set of values and is much easier to manipulate than an array;
  • the values that make up a list are called elements (just like in arrays).
package com.caffeinealgorithm.programminginjava;

import java.util.ArrayList;
import java.util.List;

public class Lists {
  public void Run() {
    List<String> colors = new ArrayList<>();

    colors.add("Blue");
    colors.add("Green");
    colors.add("Yellow");
    colors.add("Red");
    colors.add("Orange");

    colors.remove("Orange");
    // colors.clear();

    System.out.printf("Number of colors: %d\n", colors.size()); // Number of colors: 4
    System.out.printf("First color: %s\n", colors.get(0)); // First color: Blue
    System.out.printf("Last color: %s", colors.get(colors.size() - 1)); // Last color: Red
  }
}

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved