프로그래밍/java
[JAVA] Array<->Vector<->HashMap<->ArrayList 변환
승민아
2022. 6. 9. 00:02
Array<->ArrayList 변환 ( Array -> List -> ArrayList )
public static void main(String[] args) {
String[] arrColors = {"white", "orange", "black"};
List<String> lst = Arrays.asList(arrColors); // List는 인터페이스
ArrayList<String> aLst = new ArrayList(lst);
System.out.println("ArrayList contains"+aLst);
String[] temp = aLst.toArray(new String[aLst.size()]);
for(String t : temp)
System.out.print(t+" ");
}
실행 결과
Array<->Vector 방법(1)
public class Extest{
public static void main(String[] args) {
String[] arrColors = {"white","orange","black"};
List<String> lst = Arrays.asList(arrColors);
Vector<String> v = new Vector(lst);
System.out.println("Vector contains:"+v);
String[] temp = v.toArray(new String[v.size()]);
for(String t : temp)
System.out.print(t+" ");
}
}
Array<->Vector 방법(2)
public class Extest{
public static void main(String[] args) {
String[] strColors = {"white","orange","black"};
Vector<String> vColors = new Vector<String>();
Collections.addAll(vColors, strColors);
System.out.println("Vector contains: "+vColors);
String[] temp = vColors.toArray(strColors);
for(String t : temp)
System.out.print(t+" ");
}
}
HashMap의 Value -> Collection -> Array
( HashMap<String,Score> apply; )
Collection<Score> vs = apply.values(); // 예로 개인적으로 만든 Score 클래스
Score v[] = vs.toArray( new Score[apply.size()] );
for( Score x : v )
System.out.println(x);
HashMap의 Value -> Collection -> ArrayList
ArrayList<Score> lst = new ArrayList<Score>( apply.values() );