- 浏览: 76765 次
文章分类
- 全部博客 (89)
- web service (9)
- subversion (1)
- JBOSS (3)
- interview (23)
- jQery (2)
- ExtJs (0)
- Axis (0)
- Design pattern (3)
- Agile (2)
- mutithread (0)
- Core Java (24)
- programming methods (1)
- SSH (7)
- jee design (1)
- OO (4)
- books (8)
- other (1)
- JSF (7)
- seam (2)
- Weblogic (4)
- JPA (1)
- ADF (1)
- Spring (5)
- Tomcat (1)
- DWR (2)
- JEE (3)
- Servlet (1)
- EJB (1)
- JDBC (3)
最新评论
-
iloveflower:
呵呵。好好学习。。。。。。。。。。。。
java 读书 -
Eric.Yan:
看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
java 读书
There are some important things to note with the solutions given above:
Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.
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);
}
}
}
Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.
new ArrayList(Arrays.asList(myArray));
Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.
import java.util.Collections;
List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};
Collections.addAll(myList, myStringArray);
After this code, 'myList' should contain all the elements from the array.
Best regards,
Janarthan S
If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
Very good analysis and you need to add also that copying the array affects the performance of your code so if performance is an issue then user the Arrays.asList() way
发表评论
-
Java Collection summary
2012-06-16 02:40 534Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 748I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 654How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 666Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 785Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 765http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 889A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 663各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 4751、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 786import java.util.*; public c ... -
core java interview summary
2012-06-12 04:11 344http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 522Question: 1) Write code to che ... -
OO Design books -good website
2012-06-07 03:13 658I’m always on the search on goo ... -
Write a String Reverser (and use Recursion!)
2012-06-07 03:03 489Java Interview questions: Write ... -
Java高手必会的要点
2012-05-29 03:28 579http://developer.51cto.com/art/ ... -
How to use getClass().getClassLoader().getResource()
2012-05-29 03:13 1710This is the simplest wat to get ... -
How to override equals method in Java
2012-05-12 02:57 1509Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 874Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 812ArrayList in Java is most frequ ... -
How to use Comparator and Comparable in Java? With example
2012-05-12 02:21 736Read more: http://javarevisited ...
相关推荐
C# 中 Array和 ArrayList详解及区别 一、Array 的用法 type[] typename=new type[size]; 或者 type[] typename=new type[]{ }; Array类型的变量在声明的同时必须进行实例化(如果初始化至少得初始化数组...
我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类。我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和拆箱的负担,如果我们操纵的数据...
day14-ArrayList集合 1.ArrayList 1.1ArrayList类概述【理解】 ...ArrayList<String> array = new ArrayList<String>(); //添加元素 array.add("hello"); array.add("world"); array.add("java");
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析。 一、数组Array 数组是一个存储相同类型元素的固定大小的顺序集合。数组是用来存储数据的...
Element[] array = {new Element(1),new Element(2),new Element(3)}; 1.流行也是被多人接受的答案 普遍也是被多人接受的答案如下: ArrayList<Element> arrayList = new ArrayList(Arra
csv BufferReader로한읽고split split split터터터터터String String。 在ArrayList上添加ArrayList。 版本2.5 ArrayFile2.java csv BufferReader로한읽고split split split터터터터터String String。 在...
代码如下:using System.Collections; ArrayList a = new ArrayList(); a.Add(“a”);//这里”a”可以改成你要取出的字符串 a.Add(“b”); 运行后a就相当于一个... 您可能感兴趣的文章:C#中数组Array,ArrayList,泛型
// add elements to the array list al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); al.add(1, "A2"); System.out.println("Contents of al: " + al); ...
public static List jsonToList(String jsonString, Class cla, Map map) { List list = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); list = new ...
ArrayList<String> arrayList = new ArrayList<String>(); int arraySize = list.size(); for(int i=0;i<arraySize;i++){ arrayList.add(list.get(i).getCityName()); Log.i(TAG, "CityName ="+list....
ArrayList< String> squadList = new ArrayList< String> (); squadList . add(“ Bob ”); squadList . add(“ John ”); squadList . add(“ Kenn ”); squadList(“ Kevin ”); 从阵列中删除“ Kenn”。 打印...
import java.util.ArrayList; import java.util.... List<String> list = new ArrayList<String>(); list.add("luojiahui"); list.add("luojiafeng"); //方法1 Iterator it1 = list.iterator(); while(it1.has
关于各种Activity切换效果 appfinal Spinner sprAnim = (Spinner) ... List<String> list = new ArrayList<String>(); // 鑾峰彇XML涓畾涔夌殑鏁扮粍 for (int i = 0; i ; i++) { list.add(ls[i]);
ArrayList< String> squad = new ArrayList< String> (); 将四个名称添加到数组。 打印数组 练习23 创建一个整数数组。 在其中放入5个数字。 打印数字。 练习24 创建数组 ArrayList< Integer> numberList = new ...
String_Operations-Array_List 字符串操作数组列表的Java程序 这是一个使用数组列表进行字符串操作的Java程序 执行的操作: 1. Append 2. Add 3. Search 4. Find 5. Display 6. Exit
ArrayList array = new ArrayList(strInput.Length); fs = new FileStream("C:\\Temp\\input.txt", FileMode.Open); sr = new StreamReader(fs); while (false == sr.EndOfStream) { string word = sr....
Dim ValueArray As ArrayList Dim ThresHold() As Single 'HIHI、HI、LO、LOLO Dim Name As String = "参数名" Dim Unit As String = "单位" #End Region #Region "构造函数、析构函数" Public Sub New...
ArrayList<NameValuePair> postParams=new ArrayList(); postParams.add(new BasicNameValuePair("source",CONSUMER_KEY)); //封装入APP Key try { HttpPost httpRequest = new HttpPost(httpUrl); ...
ArrayList <String> list = new ArrayList(Arrays.asList(array)); 2. 从列表创建数组:列表.toArray(数组名) String[] array1 = new String[list.size()]; list.toArray(array1); 3.如果列表中的元素是可比较大小的...
Android中Spinner控件的使用,主要有二种使用方法: 一、在strings.xml文件中设置string-array标签; 二、在Activity类中实现ArrayList<String>对象,动态获取相应的数据。