Java
Array
WebDevLee
2022. 2. 5. 19:42
자바의 배열에 대해 정리하였습니다.
< Introduction >
To store a group of data
You can think of array as List in real life.
< Creating an Array Explicitly >
< Syntax >
DataType[] VariableName = {element1, element2, element3, ...};
Array is represented by [ ]
DataType : the type of data it holds.
element : Each content item inside an array.
Example)
double[] prices = {13.15, 15.87, 14.22, 16.66};
String[] clothingItems = {"Tank Top", "Beanie", "Funny Socks", "Corduroys"};
public class Newsfeed {
public Newsfeed(){
}
public String[] getTopics() {
String[] topics = {"Opinion", "Tech", "Science", "Health"};
return topics;
}
public static void main(String[] args){
Newsfeed sampleFeed = new Newsfeed();
String[] topics = sampleFeed.getTopics();
System.out.println(topics);
}
}
// [Ljava.lang.String;@2aae9190
< Importing Arrays >
To have a more descriptive printout of the array itself (not memory address), need a toString( ) method that is provided by the Arrays package in Java.
- To import Arrays package, put this code at the top of the file,
import java.util.Arrays;
- When we import a package in Java, we are making all of the methods of that package available in our code.
Example)
import java.util.Arrays;
public class Lottery(){
public static void main(String[] args){
int[] lotteryNumbers = {4, 8, 15, 16, 23, 42};
String betterPrintout = Arrays.toString(lotteryNumbers);
System.out.println(betterPrintout);
}
}
// [4, 8, 15, 16, 23, 42]
< Get Element By Index >
To access each element in an array using [ ].
- the index of an array starts at 0
Example)
int[] number = {1, 2, 3, 4, 5, 6};
System.out.println(number[1]); // 2
< Creating an Empty Array >
We can also create empty arrays and then fill the items one by one.
< Syntax >
new DataType[size]
dataType : the type of data it holds.
size : the size(length) of array.
- Empty arrays have to be initialized with a fixed size.
- Once you declare the size, it cannot be changed!
Example)
String[] menuItems = new String[5];
menuItems[0] = "Veggie hot dog";
menuItems[1] = "Potato salad";
menuItems[2] = "Cornbread";
menuItems[3] = "Roasted broccoli";
menuItems[4] = "Coffee ice cream";
// also change an item after it has been assigned
menuItems[3] = "Baked cauliflower";
// the array looks like
["Veggie hot dog", "Potato salad", "Cornbread", "Baked cauliflower", "Coffee ice cream"]
+. When we use new to create an empty array, each element of the array is initialized with a specific value depending on what type the element is:
Data Type | Initialized Value |
int | 0 |
double | 0.0 |
boolean | false |
Reference | null |
Example)
String[] my_names = new String[5];
int[] my_ages = new int[5];
: my_names contain five nulls.
: my_ages contain five 0s
< Length >
To get the length of an array using length property of the array.
Example)
double[] prices = {13.1, 15.87, 14.22, 16.66};
System.out.println(prices.length); // 4
< String[] args >
A String[] is an array made up of Strings
The args parameter is another example of a String array.
The array args contains the arguments that we pass in from the terminal when we run the class file.
Example : When we run the file 'HelloYou' in the terminal with an argument of "Laura"
java HelloYou Laura
public class HelloYou {
public static void main(String[] args) {
System.out.println("Hello " + args[0]);
}
}
// Hello Laura