Java中数组的遍历
(I)标准for循环遍历数组
例如代码片段:
int [] nums = new int [] {0,1,2,3,4,5,6,7,8,9};
for(int i=0;i<11;i++){
System.out.println(num[i]);
}
(II)for-each循环遍历数组
语法:
for(<迭代变量声明>:<数组>){
语句;
}
例如代码片段:
for(int n : nums){
System.out.println(n);
}
for(type element: array)
{
System.out.println(element);
}
For-Each循环的缺点:丢掉了索引信息。
当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息。
下面代码
首先对比了两种for循环;之后实现了用增强for循环遍历二维数组;最后采用三种方式遍历了一个List集合。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ForeachTest
{
public static void main(String[] args)
{
int[] arr = {1, 2, 3, 4, 5};
System.out.println("----------旧方式遍历------------");
//旧式方式
for(int i=0; i<arr.length; i++)
{
System.out.println(arr[i]);
}
System.out.println("---------新方式遍历-------------");
//新式写法,增强的for循环
for(int element:arr)
{
System.out.println(element);
}
System.out.println("---------遍历二维数组-------------");
//遍历二维数组
int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} ;
for(int[] row : arr2)
{
for(int element : row)
{
System.out.println(element);
}
}
//以三种方式遍历集合List
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println("----------方式1-----------");
//第一种方式,普通for循环
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
System.out.println("----------方式2-----------");
//第二种方式,使用迭代器
for(Iterator<String> iter = list.iterator(); iter.hasNext();)
{
System.out.println(iter.next());
}
System.out.println("----------方式3-----------");
//第三种方式,使用增强型的for循环
for(String str: list)
{
System.out.println(str);
}
}
}
参考:
http://javabc.baike.com/article-410951.html
http://www.cnblogs.com/mengdd/archive/2013/01/21/2870019.html
String.split 用法
它是java.lang包中的String.split()方法,返回是一个数组
我在应用中用到一些,给大家总结一下,仅供大家参考:
1、如果用“.”作为分隔的话,必须是如下写法:String.split(“\.”),这样才能正确的分隔开,不能用String.split(“.”);
2、如果用“|”作为分隔的话,必须是如下写法:String.split(“\|”),这样才能正确的分隔开,不能用String.split(“|”);
“.”和“|”都是转义字符,必须得加”\”;
3、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如:“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split(“and|or”);
使用String.split方法分隔字符串时,分隔符如果用到一些特殊字符,可能会得不到我们预期的结果。
我们看jdk doc中说明
public String[] split(String regex)
Splits this string around matches of the given regular expression.
参数regex是一个 regular-expression的匹配模式而不是一个简单的String,他对一些特殊的字符可能会出现你预想不到的结果,比如测试下面的代码:
用竖线 | 分隔字符串,你将得不到预期的结果
String[] aa = “aaa|bbb|ccc”.split(“|”);
//String[] aa = “aaa|bbb|ccc”.split(“\|”); 这样才能得到正确的结果
for (int i = 0 ; i
JAVA中int、String的类型转换
int -> String
int i=12345;
String s=”“;
第一种方法:s=i+”“;
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
String -> int
s=”12345”;
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
以下是答案:
第一种方法:s=i+”“; //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象
第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
1如何将字串 String 转换成整数 int?
A. 有两个方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
2 如何将整数 int 转换成字串 String ?
A. 有叁种方法:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = “” + i;
注: Double, Float, Long 转成字串的方法大同小异.
JAVA数据类型转换
这是一个例子,说的是JAVA中数据数型的转换.供大家学习
package shenmixiaozhu;
import java.sql.Date;
public class TypeChange {
public TypeChange() {
}
//change the string type to the int type
public static int stringToInt(String intstr)
{
Integer integer;
integer = Integer.valueOf(intstr);
return integer.intValue();
}
//change int type to the string type
public static String intToString(int value)
{
Integer integer = new Integer(value);
return integer.toString();
}
//change the string type to the float type
public static float stringToFloat(String floatstr)
{
Float floatee;
floatee = Float.valueOf(floatstr);
return floatee.floatValue();
}
//change the float type to the string type
public static String floatToString(float value)
{
Float floatee = new Float(value);
return floatee.toString();
}
//change the string type to the sqlDate type
public static java.sql.Date stringToDate(String dateStr)
{
return java.sql.Date.valueOf(dateStr);
}
//change the sqlDate type to the string type
public static String dateToString(java.sql.Date datee)
{
return datee.toString();
}
public static void main(String[] args)
{
java.sql.Date day ;
day = TypeChange.stringToDate(“2003-11-3”);
String strday = TypeChange.dateToString(day);
System.out.println(strday);
}
}
JAVA中常用数据类型转换函数
虽然都能在JAVA API中找到,整理一下做个备份。
本文介绍了Java中增强for循环遍历数组的优缺点,并展示了如何遍历二维数组和List集合。同时,讲解了String.split方法的使用,强调了特殊分隔符的处理。此外,还讨论了Java中int与String之间的转换,提供了不同方法的比较和应用场景。
,String.split 用法,Java int和String转换 foundation, working on&spm=1001.2101.3001.5002&articleId=47440291&d=1&t=3&u=ff25d357cec04f1b96023a0ceebfc07b)

被折叠的 条评论
为什么被折叠?



