一个简单的java小程序,我是初学者,不要编太难,看不懂

一个简单的java小程序,我是初学者,不要编太难,看不懂,第1张

也许是我水平太差,我觉得这不是一个小程序,还是有点难度的

首先明确子串的概念,字符串abc,共有6个子串,a,b,c,ab,bc,abc,找出两个字符串中所共有的子串,我的思路是这样的,先找到这两个字符串中相袜基槐同的字符串片段,然后,分解这些片段,这些片段分解后的子串就是两个字符串所共同的子串

import java.util.HashSet

import java.util.Iterator

import java.util.Set

import java.util.Vector

public class Strhelp {

public static Vector<String>list1=new Vector<String>()

public static Set set1 = new HashSet()

public static Set set2 = new HashSet()

/**

* 从第一个字符串的第一个字符开始,寻找str2中和str1中字符相同的字符及其位置

* @param str1

* @param str2

*/

public static void sameStr(String str1,String str2){

int l1=str1.length()

int l2=str2.length()

for(int i=0i<l1i++){

for(int j=0j<l2j++){

if(str1.charAt(i)==str2.charAt(j)){

samechar(str1,str2,i,j)

}

}

}

}

/**

* 找到相同的字符及其位置后,以此为基础,向后延长,查找告友相同的字符串

* @param s1

* @param s2

* @param index1

* @param index2

*/

public static void samechar(String s1,String s2,int index1,int index2){

int length=2

if((index1+length)<=s1.length()&(index2+length)<=s2.length()){

while(s1.substring(index1, index1+length).equals(s2.substring(index2, index2+length))){

length++

}

}

list1.add(s1.substring(index1, index1+length-1))

}

/**

* 找到的相同的字符串必然有重复的,这里将重复的消除

* @param list

*/

public static void delsmae(Vector<锋厅String>list){

for(int i=0i<list.size()i++){

set1.add(list.get(i).toString())

}

}

/**

* 遍历set,逐个分解子串

* @param s

*/

public static void zichuan(Set s){

Iterator it = s.iterator()

for(it.hasNext()){

getchuan(String.valueOf(it.next()))

}

}

/**

* 该函数用于分解字符串,查找子串

* @param str

*/

public static void getchuan(String str){

set2.add(str)

int len=str.length()-1

int begin=0

if(str.length()==1){

set2.add(str)

}else{

for(int i=0i<2i++){

getchuan(str.substring(0, len))

set2.add(str.substring(0, len))

getchuan(str.substring(1, len+1))

set2.add(str.substring(1, len+1))

}

}

}

public static void main(String[] args){

String str1="sdfsfherrshdrghjjtyasdwr"

String str2="asdfhdfhfrsadfthfsdgfhwe"

sameStr(str1,str2)

delsmae(list1)

zichuan(set1)

System.out.println(set2)//打印相同的子串

}

}

//第一题:

import java.io.BufferedReader

import java.io.IOException

import java.io.InputStreamReader

import java.util.ArrayList

import java.util.List

import java.util.ArrayList

import java.util.List

public class ManageName {

List<String>namelist = new ArrayList<String>()

public void PrintMenu() {

System.out.println("\n\r1.Add new name " + "2.Display all name"

+ " 3.Quit"

+ "\nPlease select menu(1,2,3)")

}

public void displayname() {

for (int w = 0w <this.namelist.size()w++) {

System.out.println("Name" + w+":" + this.namelist.get(w))

}

}

public List addName(String name) {

namelist.add(name)

return namelist

}

public static void main(String[] args) throws IOException {

ManageName a = new ManageName()

a.addName("jing5083394")

BufferedReader in = new BufferedReader(new InputStreamReader(System.in))

while (true) {

a.PrintMenu()

String content = in.readLine()

if (content.equalsIgnoreCase("3")) {

break

} else if (content.equalsIgnoreCase("1")) {

String name

BufferedReader in2 = new BufferedReader(new InputStreamReader(

System.in))

System.out.println("Please input the Name to add new user")

String content2 = in.readLine()

name = content2

a.addName(name)

System.out.println("Add name successfully\r\n")

}

else if (content.equalsIgnoreCase("2")) {

a.displayname()

}

else {

System.out.println("Invalid selections!\n")

}

}

}

}

//第二题: 把文件放到namefile.txt

/*

* 从namefile.txt文件中读入的字符串全部转换成大写字母,

* 再按原来的顺序输出到yoursData.txt文件中。

*/

import java.lang.Character

import java.io.*

//将一个文件复制到另一个文件中(覆盖)

public class Filestream {

public static void main(String args[]) {

try {

File inFile = new File("c:\\namefile.txt")

File outFile = new File("c:\\yoursData.txt")

FileInputStream fis = new FileInputStream(inFile)// 读输入文件

FileOutputStream fos = new FileOutputStream(outFile)

int c

while ((c = fis.read()) != -1) {

c = Character.toUpperCase(c)

fos.write(c)

}// 写入文件中

System.out.println("Output file finish")

fis.close()

fos.close()

} catch (FileNotFoundException e) {

System.out.println("FileStreamsTest: " + e)

} catch (IOException e) {

System.err.println("FileStreamsTest: " + e)

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12389833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存