- List与String[]的相互转化
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
String[] strs = list.toArray(new String[list.size()]);
for (String s : strs) {
System.out.println(s);
}
list.clear();
list = Arrays.asList(strs);
for (String s : list) {
System.out.println(s);
}
}
- ==操作
public static void main(String[] args) {
int a = 2;
int b = 2;
String s1 = "你好";
String s2 = s1;
System.out.println(a == b);
System.out.println(s1 == s2);
}
- 集合的删除操作
下面两种方法是否可以完全删除集合中的元素
public static void delete1(List<String> list) {
for (int i = 0; i < list.size(); i++) {
list.remove(i);
}
System.out.println(list.size());
if(!list.isEmpty()){
for(String element:list){
System.out.println(element);
}
}
}
public static void delete2(List<String> list) {
int size = list.size();
for (int i = 0; i < size; i++) {
list.remove(i);
}
System.out.println(list.size());
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class RemoveList {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("b");
list.add("a");
list.add("b");
list.add("c");
removwe3(list);
System.out.println(list);
}
public static void remove1(List<String> list) {
for (int i = list.size() - 1; i >= 0; i--) {
list.remove(i);
}
}
public static void removwe3(List<String> list) {
Iterator<String> it = list.iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
}
package com.h.test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("b");
list.add("a");
list.add("b");
list.add("c");
long begin = System.currentTimeMillis();
removeDuplicate3(list);
long end = System.currentTimeMillis();
System.out.println("执行时间:"+(end-begin));
}
public static void removeDuplicate(List<String> list) {
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size(); j++) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
System.out.println(list);
}
public static void removeDuplicate2(List<String> list) {
Set<String> set = new HashSet<>(list);
list.clear();
list.addAll(set);
System.out.println(list);
}
public static void removeDuplicateWithOrder(List<String> list) {
Set<String> set = new HashSet<>();
List<String> newList = new ArrayList<>();
for (Iterator<String> it = list.iterator(); it.hasNext();) {
String element = it.next();
if (set.add(element)) {
newList.add(element);
}
}
list.clear();
list.addAll(newList);
System.out.println(list);
}
public static void removeDuplicate3(List<String> list) {
List<String> newList = new ArrayList<>();
for (Iterator<String> it = list.iterator(); it.hasNext();) {
String element = it.next();
if (!newList.contains(element)) {
newList.add(element);
}
}
System.out.println(newList);
}
}
package com.h.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListDemo2 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("c");
list.add("c");
remove3(list);
}
public static void remove1(List<String> list) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("c")) {
list.remove(i);
}
}
System.out.println(list);
}
public static void remove2(List<String> list) {
for (String string : list) {
if ("c".equals(string)) {
list.remove(string);
}
}
System.out.println(list);
}
public static void remove3(List<String> list) {
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String element = it.next();
if ("c".equals(element)) {
it.remove(element);
}
}
System.out.println(list);
}
}
- Map
package com.h.collection;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
public static void main(String[] args) {
Map<String, String> map1 = new HashMap<String, String>();
Map<String, Map<String, String>> map2 = new HashMap<String, Map<String, String>>();
map1.put("ak1", "av1");
map2.put("m", map1);
map1.put("ak2", "av2");
for (Map.Entry<String, Map<String, String>> entry : map2.entrySet()) {
Map<String, String> map = entry.getValue();
String key = entry.getKey();
System.out.println("key=" + key);
for (Map.Entry<String, String> en : map.entrySet()) {
System.out.println("key=" + en.getKey());
System.out.println("value=" + en.getValue());
}
}
}
}
打印结果:
key=ak1
value=av1
key=ak2
value=av2
package com.h.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapIteration {
public static void iter1(Map<String, String> map) {
for (String key : map.keySet()) {
System.out.println("key=" + key + " value=" + map.get(key));
}
}
public static void iter2(Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key=" + entry.getKey() + " value="
+ entry.getValue());
}
}
public static void iter3(Map<String, String> map) {
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key=" + entry.getKey() + " value="
+ entry.getValue());
}
}
public static void iter4(Map<String, String> map) {
for (String value : map.values()) {
System.out.println(value);
}
}
}
- 引用传递还是值传递?
package com.h.test;
public class ReferenceTest {
public static void main(String[] args) {
int i = 0;
i++;
inc(i);
System.out.println(i);
}
public static void inc(int num) {
num++;
}
}
- 字符串反转操作
package com.h.test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class StringReverse {
public static void swap1(char[] arr) {
char temp;
int length = arr.length;
for (int i = 0; i < length / 2; i++) {
temp = arr[i];
arr[i] = arr[length - 1 - i];
arr[length - 1 - i] = temp;
}
}
public static void swap2(char[] arr, int begin, int end) {
char temp;
while (begin < end) {
temp = arr[begin];
arr[begin] = arr[end];
arr[end] = temp;
begin++;
end--;
}
}
public static String reverse2(String str) {
char[] arr = str.toCharArray();
swap2(arr, 0, arr.length - 1);
int begin = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == ' ') {
swap2(arr, begin, i - 1);
begin = i + 1;
}
}
return new String(arr);
}
public static String reverse3(String str) {
char[] arr = str.toCharArray();
if (str.contains(" ")) {
StringBuilder sb = new StringBuilder();
String[] strs = str.split(" ");
for (int i = strs.length - 1; i >= 0; i--) {
System.out.println(strs[i]);
if (i == 0) {
sb.append(strs[i]);
} else {
sb.append(strs[i] + " ");
}
}
return sb.toString();
} else {
swap2(arr, 0, arr.length - 1);
return new String(arr);
}
}
public static void reverse4(String str) {
String[] strs = str.split(" ");
List<String> list = Arrays.asList(strs);
Collections.reverse(list);
for (String string : list) {
System.out.println(string);
}
}
public static String reverse5(String str) {
char[] arr = str.toCharArray();
int begin = 0;
int end = arr.length - 1;
while (begin < end) {
arr[begin] ^= arr[end];
arr[end] ^= arr[begin];
arr[begin] ^= arr[end];
begin++;
end--;
}
return new String(arr);
}
public static String reverse6(String str) {
if (str.length() == 1) {
return str;
} else {
return reverse6(str.substring(1)) + str.charAt(0);
}
}
public static String reverse1(String str) {
StringBuilder sb = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
sb.append(str.charAt(i));
}
return sb.toString();
}
}
- 字符串的连接操作
package com.h.test;
public class StringTest {
public static void operate(StringBuffer a, StringBuffer b) {
a.append(b);
b = a;
}
public static void operate(String a, String b) {
a += b;
b = a;
}
public static void main(String[] args) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
String x = "a";
String y = "b";
operate(a, b);
operate(x, y);
System.out.println(a + "---" + b);
System.out.println(x + "---" + y);
}
}
- 常量池
package com.h.test;
public class AutoUnboxingTest {
public static void main(String[] args) {
Integer a = new Integer(3);
Integer b = 3;
int c = 3;
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(a == b);
System.out.println(a == c);
System.out.println(f1 == f2);
System.out.println(f3 == f4);
}
}
- 打印文件夹目录
要求:给定一个目录,打印出该目录包含的目录或者文件的结构来,文件和其父目录之间通过缩进来表示父子关系。
package com.h.util;
import java.io.File;
import java.util.ArrayList;
import java.util.Stack;
public class PrintFile {
private static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
File f = new File("D:/a");
print2(f, new FileNameFilter() {
@Override
public Boolean accept(String filaName) {
return true;
}
});
System.out.println("=================================");
print(f, new FileNameFilter() {
@Override
public Boolean accept(String filaName) {
return true;
}
},-2,'-');
System.out.println(sb);
}
public static void print(File file,FileNameFilter fileNameFilter,int charsToLeft,char spanChar){
if (null == file){
return;
}
charsToLeft += 2;
if (file.isFile()){
if (fileNameFilter.accept(file.getName())){
for (int i=0;i<charsToLeft;i++){
sb.append(spanChar);
}
sb.append(file.getName() + "\r\n");
}
}
if (file.isDirectory()){
for (int i=0;i<charsToLeft;i++){
sb.append(spanChar);
}
sb.append(file.getName() + "\r\n");
File[] files = file.listFiles();
if (null != files){
for (File f:files) {
print(f,fileNameFilter,charsToLeft,spanChar);
}
}
}
}
public static void print2(File file,FileNameFilter fileNameFilter){
ArrayList<FileDesc> list = new ArrayList<FileDesc>();
if (file == null){
return;
}
Stack<File> s1 = new Stack<File>();
Stack<Integer> s2 = new Stack<Integer>();
s1.push(file);
s2.push(0);
FileDesc fileDesc = null;
while (!s1.empty()){
File root = s1.pop();
Integer index = s2.pop();
if (root.isFile()){
if (fileNameFilter.accept(root.getName())){
fileDesc = new FileDesc(root.getName(),index);
list.add(fileDesc);
}
}
if (root.isDirectory()){
fileDesc = new FileDesc(root.getName()+"$",index);
list.add(fileDesc);
File[] child = root.listFiles();
if (null == child){
continue;
}
for (File f:child) {
s1.push(f);
s2.push(index+1);
}
}
}
FileDesc fileDes = null;
int fileIndex = 0;
for (int i=0;i<list.size();i++){
fileDes = list.get(i);
fileIndex = fileDes.getFileIndex();
if (fileIndex != 0){
for (int j=0;j<2*fileIndex;j++){
System.out.print("-");
}
}
System.out.println(list.get(i));
}
}
}