Java 常用操作

Convert java string to float / int

摘自:http://alvinalexander.com/blog/post/java/convert-string-float

 String s = "100.00";
  try 
  {
    float f = Float.valueOf(s.trim()).floatValue();
    System.out.println("float f = " + f);
  }
  catch (NumberFormatException nfe) 
  {
    System.err.println("NumberFormatException: " + nfe.getMessage());
  }

其中,Float.valueOf(s.trim())返回一个Float对象


或者


/**
 * A Java example class, demonstrates how
 * to convert a Java String to a Java float.
 */
public class JavaConvertStringToFloat
{
  public static void main(String[] args)
  {
    try
    {
      String string = "foo";
      float f = Float.parseFloat(string);
    }
    catch (NumberFormatException nfe)
    {
      nfe.printStackTrace();
    }
  }
}


How to convert Array [] to ArrayList in Java

转自:http://www.journaldev.com/756/how-to-convert-array-to-arraylist-in-java

Arrays.asList(T… a): This is the simplest way to convert Array to ArrayList in java but this method returns the underlying representation of the array in the form of ArrayList. The returned ArrayList is fixed-sized and any attempt to modify that will result in UnsupportedOperationException at runtime. Also any change in the array will change the elements in ArrayList also.


Collections.addAll(ArrayList strList, T[] strArr): This is the best way to convert array to ArrayList because the array data is copied to the list and both are independent object. Once the array is copied, you can modify both the objects independently.


Now let’s see both these methods usage in action.

package com.journaldev.util;
 
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
 
public class ArrayToArrayList {
 
    /**
     * This class shows different methods to convert Array to ArrayList
     * 
     * @param args
     */
    public static void main(String[] args) {
        String[] strArr = {"1", "2", "3", "4"};
        List<String> strList = new ArrayList<String>();
        //return the list representation of array
        //any change in array elements will change the arrayList elements also
        strList = Arrays.asList(strArr);
        System.out.println("Original ArrayList from Arrays.asList()");
        for (String str : strList)
            System.out.print(" " + str);
        //change the array element and see the effect is propogated to list also.
        strArr[0] = "5";
        System.out.println("\nChange in array effect on ArrayList");
        for (String str : strList)
            System.out.print(" " + str);
        //below code will throw java.lang.UnsupportedOperationException because
        // Arrays.asList() returns a fixed-size list backed by the specified array.
        //strList.add("5");
 
        strList = new ArrayList<String>();
 
        Collections.addAll(strList, strArr);
        //change both the array and arraylist and check if they are independent?
        strList.add("5");
        strArr[0] = "1";
        System.out.println("\nArray to ArrayList using Collections.addAll()");
        for (String str : strList)
            System.out.print(" " + str);
 
    }
 
}
Output of the above program is:

Original ArrayList from Arrays.asList()
 1 2 3 4
Change in array effect on ArrayList
 5 2 3 4
Array to ArrayList using Collections.addAll()
 5 2 3 4 5


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值