问题:随机生成30 ~ 100之间的随机整数?
(笔试时,写下面的其中一个方法就好,类和main函数不用写,别浪费宝贵的笔试时间)
public class 随机数 {
/**
* 问题:随机生成30 ~ 100之间的随机整数
*/
//法1(new Random().nextInt()用来获取0~n之间的随机整数)
static Integer randomInt1(){
int min = 30;
int max = 100;
int result = new Random().nextInt(max-min) + min;
return result;
}
//法2(Math.random()用于获取0~1之间的随机数)
static Integer randomInt2(){
int min = 30;
int max = 100;
int result = (int) ((Math.random() * (max - min)) + min);
return result;
}
public static void main(String[] args) {
System.out.println(randomInt1());
System.out.println(randomInt1());
}
}
问题:找出1 ~ 1000内的所有质数?
嵌套两个for循环
public class 质数 {
/**
* 问题:找出1 ~ 1000内的所有质数?(除了1和本身,不能被其他自然数整除的数)
*/
static void prime(){
for(int i = 2; i <= 1000; i++){
boolean flag = true;
for (int j = 2; j < i; j++){
if(i % j == 0){
flag = false;
break;
}
}
if(flag){
System.out.println(i);
}
}
}
public static void main(String[] args) {
prime();
}
}
问题:找出1 ~ 1000内的所有合数?
public class 合数 {
/**
* 问题:找出1 ~ 1000内的所有合数?
*/
//法1(除了1和本身,能被其它自然数整除的数)
static void composite1(){
for(int i = 2; i <= 1000; i++){
for(int j = 2; j < i; j++){
if(i % j ==0){
System.out.println(i);
break;
}
}
}
}
//法2(除了1和质数,其它的自然数都是合数)
static void composite2(){
for(int i = 2; i <= 1000; i++){
boolean flag = true;
for(int j = 2; j < i; j++){
if(i % j ==0){
flag = false;
break;
}
}
if(!flag){
System.out.println(i);
}
}
}
public static void main(String[] args) {
composite1();
composite2();
}
}
这是我在java笔试中遇到的一道编程题:写一段程序,判断字符串是否是回文字符串
看到这道题目我当时就蒙圈了,回文是啥意思?
笔试立马就上了度娘,一看才知道,原来回文就是顺着读和逆着读都是一样的,比如:abcba
OK,知道是啥意思后就好办啦
public class 字符串回文判断 {
/**
* 问题:判断字符串是否是回文字符串
*/
//法1:
static boolean palindrome1(String str) {
//通过StringBuffer将字符串反转
return str.equals(new StringBuffer(str).reverse().toString());
}
//法2:
static boolean palindrome2(String str) {
if ((str.length() <= 1) || (str.charAt(0) == str.charAt(str.length() - 1))){
return true;
}
return false;
}
//静态函数不能加载非静态函数
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
System.out.println(palindrome1(str));
System.out.println(palindrome2(str));
}
}
问题:将D:/home目录下的file.log文件复制到D:/blog目录中的debug.log(debug.log文件存在则覆盖其内容,debug.log文件不存在则先创建再写入)
如果是音频类型文件,必需要使用字节流操作(不能用字符流)。如果是文本文件,则可以使用字符流,也可使用字节流。
字节流操作:
public static void main(String[] args) {
long begin = System.currentTimeMillis();//获取拷贝文件前的系统时间
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(new File("D:/home/file.log")); //创建文件输入流对象
output = new FileOutputStream(new File("D:/blog/debug.log")); //创建文件输出流对象
byte[] buf = new byte[8192]; //定义一个字节数组作为缓冲区,并指定一次读取8192个字节
int len; //记住读入缓冲区的字节长度
while((len = input.read(buf)) != -1){ //判断文件是否读完(读完后会返回-1)
//注意不要用output.write(buf); 不然可能导致写入的文件比原文件大,如果是图片那么可能会失真。
output.write(buf, 0, len); //从buf缓存区中读取数据,从第一个字节开始读,读len个字节。
}
}catch (IOException e) {
e.printStackTrace();
}finally {
//最后记得关闭IO流
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis(); //获取文件拷贝结束时的系统时间
System.out.println(end - begin);
}
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
思路:从上到下、从左到右逐个遍历二维数组的元素,和要查找的整数进行比较。
public class Solution {
public boolean Find(int target, int[][] array) {
boolean flag = false;
for(int i = 0; i< array.length; i++){ //
for(int j = 0; j<array[0].length; j++){
if(array[i][j]==target){
flag = true;
break;
}
}
}
return flag;
}
}
给定一个常规整型有序数组,如何找出某一整数是否在这个数组中,以及该整数所对应的下标。
public class 二分查找 {
static int find(int[] array, int target){
int i;
for(i = 0; i < array.length; i++){
if(array[i] == target){
break;
}
}
return i;
}
public static void main(String[] args) {
int[] array = new int[1000];
for(int i=0; i<1000; i++){
array[i] = i;
}
System.out.println(find(array, 657));
}
}
去除字符串两边空格!
class Solution {
public static void main(String[] args){
String str = " We Are Happy! ";
System.out.println(str.trim());
}
}
去除字符串所有空格!
public static void main(String[] args){
String str = " We Are Happy! ";
System.out.println(str.replace(" ", ""));
}
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public class Solution {
public String replaceSpace(StringBuffer str) {
return str.toString().replaceAll(" ", "%20");
}
public static void main(){
StringBuffer str = new StringBuffer("We Are Happy.");
System.out.println(replaceSpace(str));
}
}
public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
if(ch == ' '){
sb.append("%20");
}else {
sb.append(ch);
}
}
return sb.toString();
}
public static void main(){
StringBuffer str = new StringBuffer("We Are Happy.");
System.out.println(replaceSpace(str));
}
}
对于一个给定的字符串,我们需要在线性(也就是O(n))的时间里对它做一些变形。首先这个字符串中包含着一些空格,就像"Hello World"一样,然后我们要做的是把着个字符串中由空格隔开的单词反序,同时反转每个字符的大小写。比如"Hello World"变形后就变成了"wORLD hELLO"。
import java.util.*;
public class Transform {
public static String trans(String s, int n){
String result = "";
String temp = "";
for(int i = 0; i < n; i++){
char ch = s.charAt(i);
if(ch >= 'a' && ch <= 'z'){ //字母是否为小写
temp += Character.toUpperCase(ch); //将小写转化为大写
}else if(ch >= 'A' && ch <= 'Z'){ //字母是否为大写
temp += Character.toLowerCase(ch); //将大写转化为小写
}else{
temp = ch + temp; //遇到空格,就将空格添加到前面
result = temp + result;
temp = "";
}
}
result = temp + result;
return result;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s = sc.nextLine();
int n = sc.nextInt();
System.out.println(trans(s, n));
}
}
}
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”
解析:使用String的replaceAll(String regex, String target)进行替换
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
String regex = "["+ s2 +"]";
System.out.println(s1.replaceAll(regex,""));
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s1 = sc.nextLine();
String s2 = sc.nextLine();
String regex = "["+ s2 +"]";
System.out.println(s1.replaceAll(regex,""));
}
}
}
24365
解析:先遍历字符串2,截取字符串2的每个字符,然后将字符串1中相同的字符全都移除(替换成空字符串)
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
for(int i=0; i < s2.length(); i++){
s1 = s1.replaceAll(s2.substring(i,i+1),"");
}
System.out.println(s1);
}
}
在英文中,我们会把一些长的名字或者短语进行缩写。例如"looks good to me"缩写为"lgtm",短语中的每个单词的首字母组成缩写。现在给出一个字符串s,字符串s中包括一个或者多个单词,单词之间以空格分割,请输出这个字符串的缩写。
class Solution{
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //开启控制台输入
String str = input.nextLine(); //获取控制台输入的字符串
if(str != null && !"".equals(str)){
String[] array = str.split(" "); //根据空格截取字符串
for(String word : array){
System.out.print(word.charAt(0)); //不换行打印单词首字母
}
}
}
}
问题描述:
jdk和jre的区别?
参考答案:
jdk:Java Development Kit(java开发工具包),是程序员使用java语言编写java程序所需的开发工具包。jdk包含了jre、编译java源码的编译器javac(Java Compiler)以及一些调试器等。(如果需要运行程序,只需要安装jre即可;但如果想要编写程序,那么就需要安装jdk)
jre:Java Runtime Environment(java运行时环境),是java程序运行所需的软件环境,jre包含了jvm、java基础类库。
jvm:Java Virtual Machine(Java虚拟机),jvm包含了解释器javai(Java Interpreter)、类加载器ClassLoader、垃圾回收器JVM GC、内存管理等。
谈谈你对类和对象的理解?
类是对象的抽象,对象是类的具体实现。举个例子:老师就可以比作类,而xxx老师则是这个类的对象。
1.问题:简述读写IO流概念:
**在程序中所有的数据都是以流的方式进行传输和保存的。**程序需要数据的时候就需要通过输入流读取数据;程序需要保存一些数据的时候就需要通过输出流完成。
2.问题:IO流类型划分:
读写IO流类型划分:
输入流 | 输出流 | |
---|---|---|
字节流 | InputStream FileInputStream BufferedInputStream |
OutputStream FileOutputStream BufferedOutputStream |
字符流 | Reader FileReader InputStreamReader BufferedReader |
Writer FileWriter OutputStreamWriter BufferedWriter |
字节流和字符流的主要区别是什么呢
注意:
所以呢:
3.问题:FileInputStream和BufferInputStream的区别?
4.问题:FileInputStream和FileReader的区别?
5.问题:InputStream和FileInputtStream?
InputStream input = new FileInputStream();
6.问题:new FileInputStream(“D:/file.txt”); 和 new FileInputStream(new File(“D:/file.txt”));的区别?
构造函数内部封装不同罢了。new FileInputStream("D:/file.txt");
最后也是会被转化成new FileInputStream(new File("D:/file.txt"));
的。
7.问题:字节流慨念?
字节流也就是俗称的二进制流,字节流在读取数据时,数据是以字节为单位进行读写操作的。也就是一个字节一个字节的读取。
8.问题:什么是字节缓冲流?为什么使用使用字节流?
首先,了解下什么是缓冲区:
电脑内存分成5个区,他们分别是堆、栈、自由存储区、全局/静态存储区和常量存储区。
栈——就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区。里面的变量通常是局部变量、函数参数等。
堆——就是那些由new分配的内存块,他们的释放编译器不去管,由我们的应用程序去控制,一般一个new就要对应一个delete.如果程序员没有释放掉,那么在程序结束后,操作系统会自动回收。
自由存储区——就是那些由malloc等分配的内存块,他和堆是十分相似的,不过它是用free来结束自己的生命的。
全局/静态存储区——全局变量和静态变量被分配到同一块内存中,在以前的C语言中,全局变量又分为初始化的和未初始化的,在C++里面没有这个区分了,他们共同占用同一块内存区。
常量存储区,这是一块比较特殊的存储区,他们里面存放的是常量,不允许修改(当然,你要通过非正当手段也可以修改)
电脑缓冲区就是预留下来的做为急用的那一部分,为暂时置放输出或输入资料的内存。
如何对缓冲区进行操作:
当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStreamReader和BufferedReader。其中最重要的类是InputStreamReader, 它是字节转换为字符的桥梁。你可以在构造器重指定编码的方式,如果不指定的话将采用底层操作系统的默认编码方式,例如GBK等。使用FileReader读取文件: