数组这样的过程是怎么生成的?

叫啥好呢啊啊 2012-06-19 09:40:25
public class Test {
public static void main(String[] args) {
double[][] d;
String s = "1,2;3,4,5;6,7,8";
String[] sFirst = s.split(";");
for(int i=0; i<sFirst.length; i++) {
String[] sSecond = sFirst[i].split(",");
for(int j=0;j<sSecond.length;j++){
System.out.print(sSecond[j] + "");
}
}
}
运行结果1 2 3 4 5 6 7 8
我想知道的是在第一个for循环的过程中数组sSecond[]的值是怎么生成的?在i循环的过程中数组中的值不会被覆盖么?
...全文
148 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
FFF9527 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

在哪里可以看到源代码
[/Quote]

JDK安装目录下有个src.zip文件就是源码包,解压就可以了...
java_fans_2012 2012-06-20
  • 打赏
  • 举报
回复
在哪里可以看到源代码
ljhhh0123 2012-06-20
  • 打赏
  • 举报
回复
不会覆盖,因为String内的值全是常量,分割那么多次,生成的String都是独立的。这是java标准规定。
FFF9527 2012-06-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Java code

java.util.regex.Splitter



Java code

/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use……
[/Quote]

错了,以上是android的源码,jdk的源码差别不大。
JDK的源码为:java.util.regex.Pattern
FFF9527 2012-06-19
  • 打赏
  • 举报
回复

java.util.regex.Splitter



/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package java.util.regex;

import java.util.ArrayList;
import java.util.List;

/**
* Used to make {@code String.split} fast (and to help {@code Pattern.split} too).
* @hide
*/
public class Splitter {
// The RI allows regular expressions beginning with ] or }, but that's probably a bug.
private static final String METACHARACTERS = "\\?*+[](){}^$.|";

private Splitter() {
}

/**
* Returns a result equivalent to {@code s.split(separator, limit)} if it's able
* to compute it more cheaply than ICU, or null if the caller should fall back to
* using ICU.
*/
public static String[] fastSplit(String re, String input, int limit) {
// Can we do it cheaply?
int len = re.length();
if (len == 0) {
return null;
}
char ch = re.charAt(0);
if (len == 1 && METACHARACTERS.indexOf(ch) == -1) {
// We're looking for a single non-metacharacter. Easy.
} else if (len == 2 && ch == '\\') {
// We're looking for a quoted character.
// Quoted metacharacters are effectively single non-metacharacters.
ch = re.charAt(1);
if (METACHARACTERS.indexOf(ch) == -1) {
return null;
}
} else {
return null;
}

// We can do this cheaply...

// Unlike Perl, which considers the result of splitting the empty string to be the empty
// array, Java returns an array containing the empty string.
if (input.isEmpty()) {
return new String[] { "" };
}

// Collect text preceding each occurrence of the separator, while there's enough space.
ArrayList<String> list = new ArrayList<String>();
int maxSize = limit <= 0 ? Integer.MAX_VALUE : limit;
int begin = 0;
int end;
while ((end = input.indexOf(ch, begin)) != -1 && list.size() + 1 < maxSize) {
list.add(input.substring(begin, end));
begin = end + 1;
}
return finishSplit(list, input, begin, maxSize, limit);
}

public static String[] split(Pattern pattern, String re, String input, int limit) {
String[] fastResult = fastSplit(re, input, limit);
if (fastResult != null) {
return fastResult;
}

// Unlike Perl, which considers the result of splitting the empty string to be the empty
// array, Java returns an array containing the empty string.
if (input.isEmpty()) {
return new String[] { "" };
}

// Collect text preceding each occurrence of the separator, while there's enough space.
ArrayList<String> list = new ArrayList<String>();
int maxSize = limit <= 0 ? Integer.MAX_VALUE : limit;
Matcher matcher = new Matcher(pattern, input);
int begin = 0;
while (matcher.find() && list.size() + 1 < maxSize) {
list.add(input.substring(begin, matcher.start()));
begin = matcher.end();
}
return finishSplit(list, input, begin, maxSize, limit);
}

private static String[] finishSplit(List<String> list, String input, int begin, int maxSize, int limit) {
// Add trailing text.
if (begin < input.length()) {
list.add(input.substring(begin));
} else if (limit != 0) { // No point adding the empty string if limit == 0, just to remove it below.
list.add("");
}
// Remove all trailing empty matches in the limit == 0 case.
if (limit == 0) {
int i = list.size() - 1;
while (i >= 0 && list.get(i).isEmpty()) {
list.remove(i);
i--;
}
}
// Convert to an array.
return list.toArray(new String[list.size()]);
}
}


大意就是呢,字符串找到分割的正则,将分割好的字符串一个个加入到一个list中,最后返回成数组。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧