上楼梯每次只能迈一阶或者两阶,有几种方法?
斐波那契数列:
public int fbnq(int n) {
if (n == 1) {
return 1;
} else if (n == 2) {
return 1;
} else {
return fbnq(n - 1) + fbnq(n - 2);
}
}
两种单例模式?
懒汉式:
public class Singleton1 {
private Singleton1() {
}
private static Singleton1 s = null;
public static Singleton1 getInstance() {
if (s == null) {
s = new Singleton1();
}
return s;
}
}
俄汉式:
public class Singleton2 {
private Singleton2() {
}
private static Singleton2 s = new Singleton2();
public static Singleton2 getInstance() {
return s;
}
}
字符排序?
冒泡排序:
for (int i = 0; i < chs.length - 1; i++) {
for (int j = 0; j < chs.length - 1 - i; j++) {
if (chs[j] > chs[j + 1]) {
char temp = chs[j];
chs[j] = chs[j + 1];
chs[j + 1] = temp;
}
}
}
选择排序:
for (int i = 0; i < chs.length - 1; i++) {
for (int j = i + 1; j < chs.length; j++) {
if (chs[j] < chs[i]) {
char temp = chs[j];
chs[j] = chs[i];
chs[i] = temp;
}
}
}
Arrays工具类排序:
Arrays.sort(chs);
获取一个字符串在另一个字符串中出现的次数?
public int getCount(String mainStr, String subStr) {
if (mainStr.length() >= subStr.length()) {
int count = 0; // 记录次数
int index = 0;
// while((index = mainStr.indexOf(subStr)) != -1){
// count++;
// mainStr = mainStr.substring(index + subStr.length());
// }
// 改进:
while ((index = mainStr.indexOf(subStr, index)) != -1) {
index += subStr.length();
count++;
}
return count;
} else {
return 0;
}
}
@Test
public void testGetCount() {
String str1 = "cdabkkcadkabkebfkabkskab";
String str2 = "ab";
int count = getCount(str1, str2);
System.out.println(count);
}
获取最大相同子串?
public String getMaxSameString(String str1, String str2) {
if (str1 != null && str2 != null) {
String maxStr = (str1.length() >= str2.length()) ? str1 : str2;
String minStr = (str1.length() < str2.length()) ? str1 : str2;
int len = minStr.length(); // 决定了比较的轮数
for (int i = 0; i < len; i++) { // 0 1 2 3 4 此层循环决定要去几个字符
for (int x = 0, y = len - i; y <= len; x++, y++) {
if (maxStr.contains(minStr.substring(x, y))) {
return minStr.substring(x, y);
}
}
}
}
return null;
}
@Test
public void testGetMaxSameSubString() {
String str1 = "abcwerthelloyuiodef";
String str2 = "cvhellobnmiodef";
String str = getMaxSameString(str1, str2);
System.out.println(str);
}
// 如果存在多个长度相同的最大相同子串:使用ArrayList
public List<String> getMaxSameSubString1(String str1, String str2) {
if (str1 != null && str2 != null) {
List<String> list = new ArrayList<String>();
String maxString = (str1.length() > str2.length()) ? str1 : str2;
String minString = (str1.length() > str2.length()) ? str2 : str1;
int len = minString.length();
for (int i = 0; i < len; i++) {
for (int x = 0, y = len - i; y <= len; x++, y++) {
String subString = minString.substring(x, y);
if (maxString.contains(subString)) {
list.add(subString);
}
}
if (list.size() != 0) {
break;
}
}
return list;
}
return null;
}
随机生成30个数,范围2-100,获取其中的质数?
ArrayList<Integer> arrayList = new ArrayList<Integer>();
ArrayList<Integer> arrayList2 = new ArrayList<Integer>();
for (int i = 0; i < 30; i++) {
int randomNum = (int) (Math.random() * (100 - 2 + 1) + 2);
arrayList.add(randomNum);
boolean flag = true;
for (int j = 2; j < Math.sqrt(randomNum); j++) { // 范围:开方
if (randomNum % j == 0) {
flag = false;
break;
}
}
if (flag) {
arrayList2.add(randomNum);
}
}
System.out.println("随机数为:\n" + arrayList.toString());
System.out.println("质数为:\n" + arrayList2.toString());
解决线程安全问题(传入公共对像作为同步监视器)?
public class WindowTest {
public static void main(String[] args) {
Window w = new Window();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
class Window implements Runnable {
private int ticket = 100;
@Override
public void run() {
for(int i = 1;i <= 100;i++){
show();
}
}
public synchronized void show() { // 此时的同步监视器是this
if (ticket > 0) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket);
ticket--;
}
}
}
List去重4种方式?
1).双for相同remove:
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
if (list.get(i) == list.get(j)) {
list.remove(j);
j--; // 因为删除,所以角标前移
}
}
}
2).list contains判断:
for (int i = 0; i < list1.size(); i++) {
if (!list2.contains(list1.get(i))) {
list2.add(list1.get(i));
}
}
3).List转Set:
HashSet<String> set = new HashSet<>(list);
list.clear();
list.addAll(set);
4).Set add返回值判断:
for (int i = 0; i < list1.size(); i++) {
if (set.add(list1.get(i))) {
list2.add(list1.get(i));
}
}
Map的4种遍历方法?
1).keySet遍历key:
for (Object in : map.keySet()) {
System.out.println(map.get(in));
}
2).迭代器遍历entry:
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
3).entrySet遍历entry
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
4).values遍历value
for (String v : map.values()) {
System.out.println(v);
}
File文件复制?
public void streamCopy() {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("D:\\a.jpg");
fileOutputStream = new FileOutputStream("D:\\b.jpg");
int len;
byte[] temp = new byte[1024];
while ((len = fileInputStream.read(temp)) != -1) {
fileOutputStream.write(temp, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void bufferedCopy() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("day26.iml"));
bos = new BufferedOutputStream(new FileOutputStream("day26_copy.iml"));
byte[] temp = new byte[1024];
int len;
while ((len = bis.read(temp)) != -1) {
bos.write(temp, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis == null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
将gbk格式的文件转换为utf-8格式存储?
public void transform() {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
isr = new InputStreamReader(new FileInputStream("gbkBook.txt"), "gbk");
osw = new OutputStreamWriter(new FileOutputStream("utf8Book.txt"));
char[] temp = new char[1024];
int len;
while ((len = isr.read(temp)) != -1) {
osw.write(temp, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (isr != null) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
TCP编程:服务器-客户端发送文件/聊天?
// 从客户端发送文件给服务端,服务端保存到本地,并返回“发送成功”给客户端。
@Test
public void client() throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8989); // 创建ServerSocket
FileInputStream fis = new FileInputStream("baby.jpg");
OutputStream os = socket.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
System.out.println("client-------1");
socket.shutdownOutput(); // 显式的关闭输出流
//接收来自于服务器端的数据,并显示
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] newBuf = new byte[10];
int newLen;
while((newLen = is.read(newBuf)) != -1){
baos.write(newBuf,0,newLen);
}
System.out.println(baos.toString());
// 关闭流(省略try-catch-finally)
is.close();
baos.close();
os.close();
fis.close();
socket.close();
}
@Test
public void server() throws IOException {
ServerSocket serverSocket = new ServerSocket(8989);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("src/babyabc.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("server-------1");
OutputStream os = socket.getOutputStream();
os.write("收到你的靓照,非常的漂亮!".getBytes()); // 服务器给客户端反馈数据
// 关闭流
os.close();
fos.close();
is.close();
socket.close();
//serverSocket.close(); // 关闭服务器
}
URL编程:下载文件?
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/examples/playgirl.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
is = urlConnection.getInputStream();
fos = new FileOutputStream("playgirl.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
System.out.println("下载成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
if (urlConnection != null)
urlConnection.disconnect();
}
获取Class的四种方式?
①Class clazz = Person.class; // 调用类的静态属性:class,不会执行静态代码块
②Person p = new Person();Class clazz2 = p.getClass(); // 调用对象的方法:getClass(),new会执行静态代码块
③Class class3 = Class.forName(“com.atguigu.test.Person”); // 调用Class的静态方法:forName(String className),会执行静态代码块
④ClassLoader cl = Reflection.class.getClassLoader();Class class4 = cl.loadClass(“com.atguigu.test.Person”); // 使用类的加载器,不会执行静态代码块
1 3 4相似,一般选3
读取配置文件的两种方式?
方式一:
FileInputStream fis = new FileInputStream("jdbc.properties");
Properties pros = new Properties();
pros.load(fis1);
String name = pros.getProperty("name");
方式二:
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc1.properties");
Properties pros = new Properties();
pros.load(is);
String name = pros.getProperty("name");