- 浏览: 80353 次
文章分类
- 全部博客 (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 575Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 791I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 692How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 715Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 825Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 801http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 936A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 702各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 5171、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 849import java.util.*; public c ... -
core java interview summary
2012-06-12 04:11 382http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 552Question: 1) Write code to che ... -
OO Design books -good website
2012-06-07 03:13 698I’m always on the search on goo ... -
Write a String Reverser (and use Recursion!)
2012-06-07 03:03 522Java Interview questions: Write ... -
Java高手必会的要点
2012-05-29 03:28 609http://developer.51cto.com/art/ ... -
How to use getClass().getClassLoader().getResource()
2012-05-29 03:13 1750This is the simplest wat to get ... -
How to override equals method in Java
2012-05-12 02:57 1539Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 907Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 875ArrayList in Java is most frequ ... -
How to use Comparator and Comparable in Java? With example
2012-05-12 02:21 767Read more: http://javarevisited ...
相关推荐
### C# 中 Array、ArrayList 和 List 的区别 在C#编程语言中,处理集合数据时,程序员经常需要根据实际需求选择合适的集合类型。本文将详细解释C#中Array、ArrayList和List之间的区别,并通过示例代码帮助理解这些...
在Java编程中,经常需要...以上是关于"StringToList和StringtoMap和StringtoObject和StringtoArray"的基本知识。理解并掌握这些转换方法对于处理JSON数据至关重要,特别是在Java编程中进行Web服务开发或者API交互时。
C#数据结构之Array、ArrayList、Hashtable、Dictionary C#中有多种数据结构可以用来存储和管理数据,今天我们将讨论四种常用的数据结构:Array、ArrayList、Hashtable和Dictionary。这些数据结构都是_Collections_...
C# 中 Array和 ArrayList详解及区别 一、Array 的用法 type[] typename=new type[size]; 或者 type[] typename=new type[]{ }; Array类型的变量在声明的同时必须进行实例化(如果初始化至少得初始化数组...
本文主要探讨了几个常用的集合类,包括Array、ArrayList、List、Hashtable、Dictionary, TValue>、Stack和Queue,以及它们的特点和应用场景。 1. **数组**(Array):数组是最基本的数据结构,它是一个固定大小的...
day14-ArrayList集合 1.ArrayList 1.1ArrayList类概述【理解】 ...ArrayList<String> array = new ArrayList<String>(); //添加元素 array.add("hello"); array.add("world"); array.add("java");
4. **转换为Array**: 如果需要将ArrayList转换回String数组,可以使用toArray()方法,但需要传递一个空的String数组作为目标。 ```java String[] newArray = new String[stringList.size()]; stringList.toArray...
例如,单维数组如`string[] str1`,二维数组如`int[,] intArray`,以及多维数组`int[, ,] intArray1`。数组在内存中是连续存储的,因此访问速度快,但一旦创建,其大小是不可变的,不支持动态扩容。 ArrayList是...
2. **动态操作**:虽然string-array本身不支持运行时的修改,但可以在代码中创建等效的`ArrayList<String>`,根据需要进行添加、删除、修改等操作。 3. **适配ListView或RecyclerView**:string-array常被用作列表...
在Java编程语言中,ArrayList是Java集合框架的重要组成部分,它属于List接口的一个具体实现,用于存储可变大小的有序对象列表。在这个“ArrayList实现对产品CRUD”的项目中,我们将探讨如何利用面向对象编程(OOP)...
ArrayList<String> list = new ArrayList(); list.add("a1"); list.add("a2"); // 转换为String数组 String[] toBeStored = list.toArray(new String[list.size()]); // 打印结果 System.out.println...
List<String> list = new ArrayList(); list.add("1"); list.add("2"); // 获取 List 大小并创建一个 String 类型的数组 final int size = list.size(); String[] arr = (String[]) list.toArray(new String[size])...
string[] stringArray = (string[])myList.ToArray(typeof(string)); ``` 11. ArrayList与List的区别 虽然ArrayList在.NET早期版本中常见,但现在推荐使用泛型集合List,因为它提供了类型安全且性能更优。例如,List...
同时,类型转换可以通过`toXxx`方法实现,例如`intValue.toLong()`将`Int`转换为`Long`。 在比较方面,Kotlin区分了`==`和`===`。`==`用于值比较,而`===`用于引用比较。例如: ```kotlin val a = 10000 val b = ...
List<String> list = new ArrayList<String>(); list.add("a1"); list.add("a2"); // 使用toArray()方法,传入一个大小等于list.size()的String数组 String[] toBeStored = list.toArray(new String[list.size()]);...
List<String> list = new ArrayList(Arrays.asList(array)); ``` - **List转Array**: 要将List转换回Array,你可以使用`toArray()`方法。首先,你需要创建一个目标类型的空数组,然后调用`toArray()`方法传入这个...
标题中的“排序排序 array to object”暗示了这个话题可能涉及数组的排序以及将排序后的数组转换成对象。在IT行业中,排序是数据处理的基础操作,常见的排序算法有冒泡排序、选择排序、插入排序、快速排序、归并排序...