Useful External Java Libraries
- DeTech Theory
- Jul 18, 2022
- 2 min read
Java has a wealth of pre-written libraries that you can easily implement into your own code with just a couple of lines. There are a variety of classes, data structures, interfaces and more to suit your programming needs.
Today, we will talk about two of the most useful libraries.
1. java.lang.Math
The Java Math class contains tons of static methods that can execute almost any math calculation. Some examples are:
Math.sin(int x);
Math.asin(int x);
//trigonometric calculations using radians
Math.abs(int x);
//returns the absolute value of 'x'
Math.exp(int x);
//returns e^x
Math.min(int x, y);
//returns the minimum of 'x' and 'y'
2. java.util.ArrayList
ArrayLists are an extremely useful data structure found in Java. They function similarly to normal arrays, except they are resizable, meaning that you do not need to instantiated the ArrayList with a set number of spots. Additionally, you do not need to remember the position of every element in the ArrayList as there is a built-in search method.
In order to declare a new ArrayList, use the following syntax. **Note: you must declare the type of the ArrayList, 'E', using class types, not primitive data types (e.g. you must use 'Integer' instead of 'int', and 'Character' instead of 'char').
ArrayList<E> myArray = new ArrayList<>();
The ArrayList class contains many methods including:
myArray.size();
//returns the length of the ArrayList
myArray.add(E element);
//adds an element of type E to the end of the ArrayList
myArray.add(int n, E element);
//adds an element of type T at index n
myArray.remove(int n);
//removes the element at index n
For more information on java.lang.Math, look here. For more information on java.util.ArrayList, look here.
To learn how to import external libraries, look here.
Until next time!
Bronwyn
static method: a method is static if it can be called directly by the class without the instantiation of an object.
Example:
Math.abs(-4);
//abs(int x) calculates the absolute value of -4, and is called by the class Math
Comments