java list用法,去除字符串数组中重复值
import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
public class ArrayTest {
public static List printMap1(String[] s) {
List<String> list = new LinkedList<String>();
List<String> list2 = Arrays.asList(s);
list.addAll(list2);
return list;
}
public static String[] array_unique(String[] a) {
// array_unique
List<String> list = new LinkedList<String>();
for(int i = 0; i < a.length; i++) {
if(!list.contains(a[i])) {
list.add(a[i]);
}
}
return (String[])list.toArray(new String[list.size()]);
}
public static void main(String[] args) {
String[] ar = {"dd","cc","ee","ff","gg","cc","ee"};
String[] s = array_unique(ar);
for(String aa : s) {
System.out.print(aa);
}
System.out.println("\n..........");
List list3 = printMap1(ar);
for(Object o : list3) {
System.out.print(o);
}
}
}
..........
ddcceeffggccee
使用 Arrays.copyOf 进行数组复制
import java.util.Arrays;
public class ArrayCopy {
public static void main(String args[]) {
System.out.printf("Before (original)\t%s%n", Arrays.toString(args));
String copy[] = Arrays.copyOf(args, 4);
System.out.printf("Before (copy)\t\t%s%n", Arrays.toString(copy));
copy[0] = "A";
copy[1] = "B";
copy[2] = "C";
copy[3] = "D";
System.out.printf("After (original)\t%s%n", Arrays.toString(args));
System.out.printf("After (copy)\t\t%s%n", Arrays.toString(copy));
}
}
Before (copy) [null, null, null, null]
After (original) []
After (copy) [A, B, C, D]
数组Array转成List的几种方法
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);
}
}
}
ace
boom
crew
dog
eon
比较傻的做法
String[] words = { ... };
List<String> list = new ArrayList<String>(words.length);
for (String s : words) {
list.add(s);
}
这方法也还可以
import java.util.Collections;
List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};
Collections.addAll(myList, myStringArray);
给出一个字符串,找出相同字母的次数
import java.util.HashMap;
public class TestArray {
/**
*
* @param args
*/
public static void main(String[] args) {
String inputstr = "abcbssssssssssss";
char[] array_input = inputstr.toCharArray();
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(int i=0;i<array_input.length;i++){
Character row = array_input[i];
//System.out.println(row);
if(map.containsKey(row)){//包含key value值加1
Integer count = map.get(array_input[i])+1;
map.remove(row);
map.put(row, count);
}else{
map.put(row, 1);
}
}
System.out.println(map);
}
}
{s=12, b=2, c=1, a=1}
字符串匹配算法
public class CompareStr {
public static void main(String args[]) {
System.out.println(getMaxMatch("hellokingfly", "kingheart"));
}
public static String getMaxMatch(String a, String b) {
StringBuffer tmp = new StringBuffer();
String maxString = "";
int max = 0;
int len = 0;
char[] aArray = a.toCharArray();
char[] bArray = b.toCharArray();
int posA = 0;
int posB = 0;
while (posA < aArray.length - max) {
posB = 0;
while (posB < (bArray.length - max)) {
if (aArray[posA] == bArray[posB]) {
len = 1;
tmp = new StringBuffer();
tmp.append(aArray[posA]);
while ((posA + len < aArray.length)
&& (posB + len < bArray.length)
&& (aArray[posA + len] == bArray[posB + len])) {
tmp.append(aArray[posA + len]);
len++;
}
if (len > max) {
max = len;
maxString = tmp.toString();
}
}
posB++;
}
posA++;
}
return maxString;
}
}
king
ArrayList的线程安全与不安全对比演示
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class ThreadSafeDemo {
public static int demo(final List list, final int testCount) throws InterruptedException {
ThreadGroup group = new ThreadGroup(list.getClass().getName() + "@" + list.hashCode());
final Random rand = new Random();
Runnable listAppender = new Runnable() {
public void run() {
try {
Thread.sleep(rand.nextInt(2));
} catch (InterruptedException e) {
return;
}
list.add("0");
}
};
for (int i = 0; i < testCount; i++) {
new Thread(group, listAppender, "InsertList-" + i).start();
}
while (group.activeCount() > 0) {
Thread.sleep(10);
}
return list.size();
}
public static void main(String[] args) throws InterruptedException {
List unsafeList = new ArrayList();
List safeList = Collections.synchronizedList(new ArrayList());
final int N = 10000;
for (int i = 0; i < 10; i++) {
unsafeList.clear();
safeList.clear();
int unsafeSize = demo(unsafeList, N);
int safeSize = demo(safeList, N);
System.out.println("unsafe/safe: " + unsafeSize + "/" + safeSize);
}
}
}
unsafe/safe: 9999/10000
unsafe/safe: 10000/10000
unsafe/safe: 10000/10000
unsafe/safe: 10000/10000
unsafe/safe: 9999/10000
unsafe/safe: 10000/10000
unsafe/safe: 10000/10000
unsafe/safe: 9999/10000
unsafe/safe: 10000/10000
unsafe/safe: 10000/10000