using Arrays.asList
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class StringArrayTest
{
public static void main(String[] args)
{
String[] words = {"ace", "boom", "crew", "dog", "eon"};
List<String> wordList = Arrays.asList(words);
for (String e : wordList)
{
System.out.println(e);
}
}
}
String array 的一些用法
ublic class Program { public static void main(String[] args) { // This string array contains three values. String[] values = { "cat", "dog", "spider" }; // Combine these strings together. String joined = String.join(",", values); System.out.println(joined); // Separate the combined string with split. String[] values2 = joined.split(","); for (String v : values2) { System.out.println(v); } } } Output cat,dog,spider cat dog spider
import java.util.Arrays; public class Program { public static void main(String[] args) { // Create a string array with four values. String[] values = new String[4]; values[0] = "zoo"; values[1] = "marina"; values[2] = "amphitheatre"; values[3] = "colloseum"; // Sort the strings. Arrays.sort(values); // Display the sorted strings. for (String v : values) { System.out.println(v); } } } Output amphitheatre colloseum marina zoo
================================================================
ArrayList是JAVA當中的一個類別與Array差在於因為他是一個寫好的類別,有很多可以直接用的程式碼,所以很方便使用方法:1.建構ArrayList<String> myList = new ArrayList<String>(); //指定是String的型態ArrayList myList = new ArrayList(); // 也可以不指定2.加入元素String s = new String();myList.add(s);3.查詢list大小int theSize = myList.size();4.查詢特定元素boolean isIn = myList.contains(s); //若用上面的例子 因為有s字串 所以回傳true5.查詢特定元素位置int idx = myList.indexOf(s); //會回傳0 表第0個位置6.判斷List是否為空boolean empty = myList.isEmpty(); //因為有一個元素 會回傳false7.刪除特定元素myList.remove(s);
沒有留言:
張貼留言