[JAVA] [Practice] ArrayList

◎ Rewrite 2D array to ArrayList

package renshuu;
import java.util.ArrayList;
public class Array_pra {

	public static void main(String[] args) {
		//Instantiate an Array list with new
		ArrayList<String> seasons=new ArrayList<String>();
        //↑ The left side is List<String>seasons but ok
		seasons.add("spring");
		seasons.add("summer");
		seasons.add("autumn");
		seasons.add("winter");

		for (int i = 0; i < seasons.size(); i++) {
			System.out.print("\"" + seasons.get(i) + "\" ");


//Execution result
"spring" "summer" "autumn" "winter" 
		}
	}
}

◎ Rewrite the above from a for statement to a for extended statement

package renshuu;
import java.util.ArrayList;
public class for_ver {

	public static void main(String[] args) {

		ArrayList<String> seasons=new ArrayList<String>();
		seasons.add("spring");
		seasons.add("summer");
		seasons.add("autumn");
		seasons.add("winter");

		for(String a:seasons) {
			System.out.print("\""a+"\" ");
		}
	}
}

//Execution result
"spring" "summer" "autumn" "winter" 

◎ Output ArrayList using Iterator

package renshuu;

import java.util.ArrayList;
import java.util.Iterator;

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

		ArrayList<String> seasons=new ArrayList<String>();
		seasons.add("spring");
		seasons.add("summer");
		seasons.add("autumn");
		seasons.add("winter");

		Iterator<String>itSeason=seasons.iterator();
		while(itSeason.hasNext()) {
			String PSeason=itSeason.next();
			System.out.print("\""+PSeason+"\" ");
		}
	}
}

//Execution result
"spring" "summer" "autumn" "winter" 

◎ Rewrite 2D array to Array


package renshuu;
import java.util.ArrayList;
import java.util.List;
public class Nijigen_Array {

	public static void main(String[] args) {

		ArrayList<ArrayList<String>> Niji=new ArrayList<ArrayList<String>>();
		    ArrayList<String> a = new ArrayList<String>();
		    ArrayList<String> b = new ArrayList<String>();
		    
		a.add("spring");
		a.add("summer");
		a.add("autumn");
		a.add("winter");

		b.add("Cherry Blossoms");
		b.add("Umi");
		b.add("stomach");
		b.add("Kotatsu");

		Niji.add(a);
		//Niji[0]A to the zeroth element(spring··)Enter
		Niji.add(b);
		//Niji[1]B in the first element(Cherry Blossoms··)Enter

		for (List<String> datas : Niji) { //Niji[0]Take out the 0th element of Niji
			for (String data : datas) { //Take out the 0th one one by one
				System.out.print("\"" + data + "\" ");
				} //Niji[0]I finished taking out all the inside, so I went through the loop
			System.out.println(""); //Start a new line and return to the beginning of the for statement. Niji[1]To process

				}
           //↓ Line breaks between spring and Sakura lines
			System.out.println("");
		}
	}
}

//Execution result
"spring" "summer" "autumn" "winter" 
"Cherry Blossoms" "Umi" "stomach" "Kotatsu" 

Recommended Posts

[Practice] ArrayList
[Practice] ArrayList
ArrayList practice
ArrayList
Array practice
Array practice 2
[Practice] Enumeration
ArrayList class
if else practice
String Buffer practice
LinkedList and ArrayList
java practice part 1
[Practice] Map method