Programming (EN)

[Java] How to access data using array, ArrayList, and map

2020-12-19

I'm sometimes confused how to access data when I use array, arraylist, and map at the same time. So I organized my thought about it.


How to access data using array

let's see how to access data from array.


Declaring array

Data_types[] array_name = new Data_types[];

//Example
String[] prefecture = new String[8];


How to access data from array

  • variables_name[〇〇]→ take 〇〇th variables from array
  • variable_name.length→take length of array.




import java.util.*;

public class Main {
    public static void main(String[] args){

        //Declaring array
        String[] prefecture = new String[8];
                
        //Storing variables
        prefecture[0] = "Tokyo";
        prefecture[1] = "Hokkaido";
        prefecture[2] = "Okinawa";  
     
        
        //output data
        System.out.println(prefecture[0]);
        
        //Output array length
        System.out.println(prefecture.length);
        
        //output using for loop
        for(int i=0; i<prefecture.length; i++) {
            System.out.println(prefecture[i]);
        }
    }
}


How to access data from ArrayList

Here's how to declare arrayList and access data.


Declaring ArrayList

List<data_types> list_name = new ArrayList<>();

//Example
List<String> prefecture = new ArrayList<>();


Indices doesn't have to be defined unlike array because ArrayList is a variable length Collection class.


How to access data from ArrayList

  • Storing data to ArrayList:List.add(Objects);
  • Obtaining 〇〇th data from ArrayList:List.get(〇〇);
  • Obtaining length of lists:List.size();

ArrayList can only store objects, not primitives like int, float, double.





import java.util.*;

public class Main {
    public static void main(String[] args){

        //Declaring ArrayList
        List<String> prefecture = new ArrayList<>();
                
        //Storing data
        prefecture.add("Tokyo");
        prefecture.add("Hokkaido");
        prefecture.add("Okinawa");
        
        //Output data
        System.out.println(prefecture.get(0));
        System.out.println(prefecture.get(1));
        System.out.println(prefecture.get(2));
        
        //Output length of ArrayList
        System.out.println(prefecture.size());
        
        //Output using for loops
        for(int i=0; i<prefecture.size(); i++) {
            System.out.println(prefecture.get(i));
        }
    }
}


How to access data using Map

Here's how to declare and access data from Map.


Declaring Map

Map<Data_type, Data_type> Map_name = new HashMap<>()

//Example
  Map<String, String> languages = new HashMap<>();

Map is needed to be defined two data types as it store two types of data as the pair : Key and Value.


How to access data from Map

  • Adding data to Map:Map.put(key, value);
  • Obtaining data from Map:Map.get(key);
  • Obtaining Map length from Map:Map.size();




import java.util.*;

public class Main {
    public static void main(String[] args){

        //declaring Map
        Map<String, String> languages = new HashMap<>();
                
        //Storing data
        languages.put("US", "English");
        languages.put("Japan", "Japanese");
        languages.put("France", "French");
        
        //Output data
        System.out.println(languages.get("US"));
        System.out.println(languages.get("Japan"));
        System.out.println(languages.get("France"));
        
        //Output length of Map
        System.out.println(languages.size());
        
        //Output key using for loops
        for (String key : languages.keySet()) {
			System.out.println(key);
		}
		
		//Outpuut value using for loops
		for (String value : languages.keySet()) {
			System.out.println(value);
		}
    }
}

-Programming (EN)