字符串与数组

来源:互联网 发布:简单点美工软件 编辑:程序博客网 时间:2024/05/14 02:45

字符串相关代码

1、将大写变为小写,小写变为大写

import java.util.Scanner;public class DaXiao {    public static void main(String[] args) {        Scanner sca=new Scanner(System.in);        String s = sca.nextLine();        String str = "";        for (int i = 0; i < s.length(); i++) {//遍历字符串            char c = s.charAt(i);            if (Character.isLowerCase(c)) {// 判断是否为小写                c = Character.toUpperCase(c);//将小写字母变为大写字母            } else {                c = Character.toLowerCase(c);//将大写字母变为小写字母            }            str = str.concat("" + c);        }        System.out.println(str);    }}

除此之外,还可以用字母的ASCII码来判断其大小写。

import java.util.Scanner;public class DaXiao2 {    public static void main(String[] args) {        Scanner sca=new Scanner(System.in);        String s = sca.nextLine();        String str = "";        for (int i = 0; i < s.length(); i++) {            char c = s.charAt(i);            if (c >= 'a' && c <= 'z') {                c = (char) (c - 32);            } else if (c >= 'A' && c <= 'Z') {                c = (char) (c + 32);            }            str = str.concat("" + c);        }        System.out.println(str);    }}

这里写图片描述

2、字符串分割

public class StrBuf {    public static void main(String[] args) {        //将数字字符串用“,”号分隔开,从右开始        StringBuffer str=new StringBuffer("1234567890");        for (int i = str.length()-3; i>0 ; i-=3) {            str.insert(i, ',');//用insert方法插入        }        System.out.println(str);    }}

这里写图片描述

3、字符串的对比(比较邮箱的格式是否正确)

将邮箱分为四部分 @及@前后两部分和域名,按照每一部分的格式进行比较,比较合格后,进行下一部分比较。

import java.util.Scanner;public class YouXiang {    public static void main(String[] args) {        while (true) {            Scanner sca = new Scanner(System.in);            String str = sca.nextLine();            if (str.contains("@")) {                if (str.indexOf('@') == str.lastIndexOf('@')) {                    System.out.println("@合法");                    if (str.endsWith(".com") | str.endsWith(".cn")) {                        System.out.println("域名合法");                        String start = str.substring(0, str.indexOf('@'));                        String middle = str.substring(str.indexOf('@') + 1, str.lastIndexOf('.'));                        boolean boo = true;                        for (int j = 0; j < start.length(); j++) {                            char c = start.charAt(j);                            if (!(Character.isLetter(c) || Character.isDigit(c) || c == '_')) {                                boo = false;                                System.out.println("start错误");                                break;                            }                        }                        if (boo) {                            System.out.println("start合法");                            boolean boo1 = true;                            for (int i = 0; i < middle.length(); i++) {                                char c1 = middle.charAt(i);                                if (!(Character.isLetter(c1) || Character.isDigit(c1) || c1 == '_')) {                                    boo1 = false;                                    System.out.println("middle错误");                                    break;                                }                            }                            if (boo1) {                                System.out.println("middle合法");                                System.out.println("邮箱合法");                            }                        }                    } else {                        System.out.println("域名错误");                    }                }            } else {                System.out.println("@错误");                System.out.println("邮箱错误");            }        }    }}

这里写图片描述

字符串中==与equal的区别

对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同。
1、“==”比较两个变量本身的值,即两个对象在内存中的首地址。
2、“equals()”比较字符串中所包含的内容是否相同

public class BiJiao {    public static void main(String[] args) {        String s1=new String("string");        String s2=new String("string");        String s3="string";        String s4="string";        System.out.println(s1==s2);        System.out.println(s1==s3);        System.out.println(s3==s4);        System.out.println(s1.equals(s2));        System.out.println(s1.equals(s3));        System.out.println(s3.equals(s4));    }}

这里写图片描述

数组相关代码

1、二维数组的基本方法

public class ErWeiShuZu {    public static void main(String[] args) {        // 小写变大写,大写变小写        String s = "abc,ABC125";        String da = s.toLowerCase();        String xiao = s.toUpperCase();        System.out.println(da);        System.out.println(xiao);        String fen[] = s.split(",");        for (int i = 0; i < fen.length; i++) {            System.out.println(fen[i]);        }        int[][] array = new int[][] { { 1, 2 }, { 2, 3, 4 }, { 3, 4 } };        array[0] = new int[] { 1, 2, 3 };        array[1] = new int[] { 1, 2, 1 };        array[2] = new int[] { 3, 2, 3 };        for (int i = 0; i < array.length; i++) {            for (int j = 0; j < array[i].length; j++) {                System.out.print(array[i][j] + " ");            }            System.out.println();        }    }}

2、数组的复制

import java.util.Arrays;public class FuZhi {    public static void main(String[] args) {        int scores[]=new int[]{8,6,9,4,3,3,5,2,4,2};            //int scores1[]=Arrays.copyOf(scores, 3);        int scores2[]=Arrays.copyOfRange(scores, 2, 5);         for (int i = 0; i < scores2.length; i++) {            System.out.println(scores2[i]);        }    }}

3、冒泡排序问题

public class MaoPao {    public static void main(String[] args) {        int scores[] = new int[] { 8, 6, 9, 4, 3 };        int temp = 0;        for (int i = 0; i < scores.length; i++) {            for (int j = 0; j < scores.length - i - 1; j++) {                if (scores[j] > scores[j + 1]) {                    temp = scores[j];                    scores[j] = scores[j + 1];                    scores[j + 1] = temp;                }            }        }        for (int j = 0; j < scores.length; j++) {            System.out.println(scores[j]);        }    }}

这里写图片描述

4、产生十个两位随机数并进行从小到大排序

import java.util.Arrays;import java.util.Random;public class SuiJI {    public static void main(String[] args) {        int shuZu[]=new int[10];        for (int i = 0; i < 10; i++) {            Random r = new Random();            int m = r.nextInt(100);//int m = r.nextInt(90)+10;            if(m<10){                m = r.nextInt(100);                i--;            }            System.out.println("第"+(i+1)+"个随机数为:"+m);               shuZu[i]=m;        }        Arrays.sort(shuZu);        System.out.println("该数组排序之后为:");        for (int j = 0; j < shuZu.length; j++) {            System.out.print(shuZu[j]+" ");        }    }}

这里写图片描述

5、对数组进行填充

import java.util.Arrays;public class TianChong {    public static void main(String[] args) {        int scores[]=new int[]{8,6,9,4,3,3,5,2,4,2};        //Arrays.sort(scores);  升序排列        Arrays.fill(scores, 0,2, 11);        for (int i = 0; i < scores.length; i++) {            System.out.print(scores[i]);        }    }}

这里写图片描述

6、对一个NxN数组,计算对角线的乘积

import java.util.Random;import java.util.Scanner;public class DuiJiao {    public static void main(String[] args) {        Random ran=new Random();        Scanner sca=new Scanner(System.in);        int a=sca.nextInt();        int array[][]=new int [a][a];        for (int i = 0; i < a; i++) {            for (int j = 0; j < a; j++) {                array[i][j]=ran.nextInt(9)+1;            }        }        for (int i = 0; i < array.length; i++) {            for (int j = 0; j < array.length; j++) {                System.out.print(array[i][j]+"  ");            }            System.out.println();        }        int d1=1;        int d2=1;        for (int i = 0; i < a; i++) {            d1=d1*array[i][i];            d2=d2*array[i][a-i-1];        }        System.out.println("对角线乘积为:"+d1);        System.out.println("副对角线乘积为:"+d2);    }}

这里写图片描述

0 0
原创粉丝点击