Tip: Importing External Libraries in Java
- DeTech Theory
- Jul 12, 2022
- 1 min read
Interested in using external classes and methods? Don't forget to import the necessary external libraries!
Let's use the ArrayList data structure as an example:
ArrayList<Integer> intRay = new ArrayList<>();
If you try to run a program using ArrayList, however, you will get an error. In order to use this data structure, you must import the ArrayList class to your file. The general format for importing external libraries is:
//import PACKAGE.NAME.CLASS;
Many of the most useful java classes are found in java.util (this would be the PACKAGE.NAME) . The CLASS in this instance would be ArrayList
So at the top of your file, include the code snippet:
import java.util.ArrayList;
If you want to import all classes of a package, use an asterix:
import java.util.*;
It's as simple as that!
Best,
Bronwyn
Comments