栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA学习

 第十八天

循环队列

package basic;


public class CircleIntQueue {

	
	private static final int MAX_SIZE = 10;

	
	int[] data;

	
	int front;

	
	int rear;

	
	public CircleIntQueue() {
		data = new int[MAX_SIZE];
		front = 0;
		rear = 0;
	}// Of the first constructor

	@Override
	public String toString() {
		String resulString = "";
		if (front == rear) {
			return "empty";
		} // Of if
		for (int i = front; i < rear; i++) {
			resulString += data[i % MAX_SIZE] + " ";
		} // Of for
		return resulString;
	}// Of toString

	
	public void enqueue(int paraValue) {
		if ((rear + 1) % MAX_SIZE == front) {
			System.out.println("Queue full.");
			return;
		} // Of if
		data[rear % MAX_SIZE] = paraValue;
		rear++;
	}// Of enqueue

	
	public int dequeue() {
		int resultValue;
		if (rear == front) {
			System.out.println("Queue empty.");
			return -1;
		} // Of if
		resultValue = data[front % MAX_SIZE];
		front++;
		return resultValue;
	}// Of dequeue

	
	public static void main(String args[]) {
		CircleIntQueue tempQueue = new CircleIntQueue();
		System.out.println("Initialized, the list is: " + tempQueue.toString());

		for (int i = 0; i < 5; i++) {
			tempQueue.enqueue(i + 1);
		} // Of for i
		System.out.println("Enqueue, the queue is: " + tempQueue.toString());

		int tempValue = tempQueue.dequeue();
		System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());

		for (int i = 0; i < 6; i++) {
			tempQueue.enqueue(i + 10);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i

		for (int i = 0; i < 3; i++) {
			tempValue = tempQueue.dequeue();
			System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
		} // Of for i

		for (int i = 0; i < 6; i++) {
			tempQueue.enqueue(i + 100);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i
	}// Of main
}// Of class CircleIntQueue
package basic;


public class CircleCharQueue {

	
	private static final int MAX_SIZE = 10;

	
	char[] data;

	
	int front;

	
	int rear;

	
	public CircleCharQueue() {
		data = new char[MAX_SIZE];
		front = 0;
		rear = 0;
	}// Of the first constructor

	@Override
	public String toString() {
		String resulString = "";
		if (front == rear) {
			return "empty";
		} // Of if
		for (int i = front; i < rear; i++) {
			resulString += data[i % MAX_SIZE] + " ";
		} // Of for
		return resulString;
	}// Of toString

	
	public void enqueue(char paraValue) {
		if ((rear + 1) % MAX_SIZE == front) {
			System.out.println("Queue full.");
			return;
		} // Of if
		data[rear % MAX_SIZE] = paraValue;
		rear++;
	}// Of enqueue

	
	public char dequeue() {
		char resultValue;
		if (rear == front) {
			System.out.println("Queue empty.");
			return '';
		} // Of if
		resultValue = data[front % MAX_SIZE];
		front++;
		return resultValue;
	}// Of dequeue

	
	public static void main(String args[]) {
		CircleCharQueue tempQueue = new CircleCharQueue();
		System.out.println("Initialized, the list is: " + tempQueue.toString());

		for (char i = '0'; i < '5'; i++) {
			tempQueue.enqueue(i);
		} // Of for i
		System.out.println("Enqueue, the queue is: " + tempQueue.toString());

		char tempValue = tempQueue.dequeue();
		System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());

		for (char i = 'a'; i < 'f'; i++) {
			tempQueue.enqueue(i);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i

		for (int i = 0; i < 3; i++) {
			tempValue = tempQueue.dequeue();
			System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
		} // Of for i

		for (char i = 'A'; i < 'F'; i++) {
			tempQueue.enqueue(i);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i
	}// Of main
}// Of class CircleCharQueue

第十九天

" 正常打印引号

substring() 方法返回字符串的子字符串,String的方法,在自定义的MyString中想要使用该方法,要重构造一个MyString方法,才能在后面调用。

public String substring(int beginIndex) 
或

public String substring(int beginIndex, int endIndex)
  • beginIndex -- 起始索引(包括), 索引从 0 开始。

  • endIndex -- 结束索引(不包括)。

public class RunoobTest {
    public static void main(String args[]) {
        String Str = new String("This is text");
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4) );
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4, 10) );
    }
}

//返回值 : is text
//返回值 : is te

charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。

public class Test {
    public static void main(String args[]) {
        String s = "www.runoob.com";
        char result = s.charAt(6);
        System.out.println(result);
    }
}

//输出n

 

 

package basic;


public class MyString {

	
	private final static int MAX_LENGTH = 10;

	
	int length;

	
	char data[];

	
	public MyString() {
		// TODO Auto-generated constructor stub
		length = 0;
		data = new char[MAX_LENGTH];
	}// Of the first construct

	
	public MyString(String paraString) {
		// TODO Auto-generated constructor stub
		length = paraString.length();
		data = new char[MAX_LENGTH];

		// Copy data
		for (int i = 0; i < paraString.length(); i++) {
			data[i] = paraString.charAt(i);
		} // Of for
	}// Of the second construct

	@Override
	public String toString() {
		String resuString = "";
		for (int i = 0; i < length; i++) {
			resuString += data[i]; // 不需要加,
		}
		return resuString;
	}

	
	public int locate(MyString paraMyString) {
		boolean tempMatch = false;
		for (int i = 0; i < length - paraMyString.length + 1; i++) {
			tempMatch = true;
			for (int j = 0; j < paraMyString.length; j++) {
				if (data[i + j] != paraMyString.data[j]) {
					tempMatch = false;
					break;
				}
			} // 如果把下面的if写在第二个for里面则没有对比完,只要其中有一个一样就会输出当前的i
			if (tempMatch == true) {
				return i;
			}
		}
		return -1;
	}

	
	public MyString substring(int paraStartPosition, int paraLength) {
		if (paraStartPosition + paraLength > length) {
			System.out.println("The bound is exceeded.");
			return null;
		} // Of if
		MyString resultString = new MyString();// 需要无参构造一个函数
		resultString.length = paraLength;// 无参构造函数的初始值为0,要重新初始化赋值。
		for (int i = 0; i < paraLength; i++) {
			resultString.data[i] = data[paraStartPosition + i];
		} // Of for
		return resultString;
	}

	
	public static void main(String args[]) {
		MyString tempFirstString = new MyString("I like ik.");
		MyString tempSecondString = new MyString("ik");
		int tempPosition = tempFirstString.locate(tempSecondString);
		System.out.println(
				"The position of "" + tempSecondString + "" in "" + tempFirstString + "" is: " + tempPosition);

		MyString tempThirdString = new MyString("ki");
		tempPosition = tempFirstString.locate(tempThirdString);
		System.out.println(
				"The position of "" + tempThirdString + "" in "" + tempFirstString + "" is: " + tempPosition);

		tempThirdString = tempFirstString.substring(1, 2);
		System.out.println("The substring is: "" + tempThirdString + """);

		tempThirdString = tempFirstString.substring(5, 5);
		System.out.println("The substring is: "" + tempThirdString + """);

		tempThirdString = tempFirstString.substring(5, 6);
		System.out.println("The substring is: "" + tempThirdString + """);
	}// Of main
}// Of class MyString

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号