自己写的简单的arraylist

--------------------------------------------题记

最近朋友有面试,叫我帮他根据需求写一个类似arraylist的集合:remove、get、tostring、add和删除后有空的位置后面的元素自动向前移。


---------------------------------------------代码

package hxq;

public class Test {
	
	public static void main(String[] args) {
		myArrayList<String> list = new myArrayList<String>();
		list.add("1");
		list.add("2");
		
		System.out.println("list的toString:" + list.toString());
		
		System.out.println("list的第一个元素:" + list.get(0));
		
		System.out.println("list的size:" + list.size());
		
		System.out.println("删除第一个元素");
		list.remove(0);
		
		System.out.println("list的第一个元素:" + list.get(0));
		
		System.out.println("list的size:" + list.size());
		
	}
	
}

class myArrayList<T> {
	
	private Object[] myArray;
	
	/**
	 * 增加
	 * @param ts 添加的对象
	 */
	public void add(T...ts) {
		addArrayLength();
		int len = myArray.length - 1;
		if (ts.length == 1) {
			myArray[len] = ts[0];
			return ;
		}
		myArray[len] = ts;
	}
	
	/**
	 * 集合大小
	 * @return 返回集合大小
	 */
	public int size() {
		return myArray.length;
	}
	
	/**
	 *  删除集合
	 * @param index 删除的索引
	 */
	public void remove(int index) {
		Object[] temps = new Object[myArray.length];
		int j = 0;
		for (int i = 0; i < myArray.length; i++) {
			if (i != index) {
				temps[j] = myArray[i];
				j++;
			}
		}
		
		reduceArrayLength();
		for (int i = 0; i < temps.length - 1; i++) {
			myArray[i] = temps[i];
		}
	}
	
	/**
	 * 重写toString
	 */
	public String toString() {
		String str = "[";
		for (int i = 0; i < myArray.length; i++) {
			if (i > 0) {
				str += "," + myArray[i];
				continue;
			}
			str += myArray[i];
		}
		str += "]";
		
		return str;
	}
	
	@SuppressWarnings("unchecked")
	public T get(int index) {
		return (T) myArray[index];
	}
	
	//utils---------------------------------
	private void addArrayLength() {
		if (myArray == null) {
			myArray = new Object[1];
			return ;
		}
		Object[] objs = new Object[myArray.length + 1];
		for (int i = 0; i < myArray.length; i++) {
			objs[i] = myArray[i];
		}
		myArray = objs;
	}
	
	private void reduceArrayLength() {
		myArray = new Object[myArray.length - 1];
	}
	
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个简单的正则表达式引擎,可以分为以下几个步骤: 1. 将正则表达式转换为后缀表达式 2. 使用后缀表达式匹配输入字符串 以下是一个简单的实现示例: ```java import java.util.ArrayList; import java.util.List; import java.util.Stack; public class SimpleRegexEngine { private String regex; private List<String> postfix; public SimpleRegexEngine(String regex) { this.regex = regex; this.postfix = new ArrayList<>(); convertToPostfix(); } private void convertToPostfix() { Stack<Character> stack = new Stack<>(); for (int i = 0; i < regex.length(); i++) { char c = regex.charAt(i); if (isOperator(c)) { while (!stack.isEmpty() && priority(stack.peek()) >= priority(c)) { postfix.add(stack.pop().toString()); } stack.push(c); } else if (c == '(') { stack.push(c); } else if (c == ')') { while (!stack.isEmpty() && stack.peek() != '(') { postfix.add(stack.pop().toString()); } stack.pop(); } else { postfix.add(Character.toString(c)); } } while (!stack.isEmpty()) { postfix.add(stack.pop().toString()); } } private boolean isOperator(char c) { return c == '|' || c == '*' || c == '+'; } private int priority(char c) { switch (c) { case '|': return 1; case '+': case '*': return 2; default: return 0; } } public boolean match(String input) { Stack<Boolean> stack = new Stack<>(); for (String token : postfix) { char c = token.charAt(0); if (isOperator(c)) { boolean b1 = stack.pop(); boolean b2 = c == '|' ? stack.pop() : true; stack.push(b1 || b2); } else if (c == '+') { stack.push(stack.pop() && matchChar(input, stack.size())); } else if (c == '*') { stack.push(true); while (matchChar(input, stack.size())) { stack.pop(); stack.push(true); } } else { stack.push(matchChar(input, stack.size())); } } return stack.pop() && input.length() == stack.size(); } private boolean matchChar(String input, int index) { if (index >= input.length()) { return false; } char c = input.charAt(index); return regex.charAt(index) == '.' || regex.charAt(index) == c; } public static void main(String[] args) { SimpleRegexEngine regexEngine = new SimpleRegexEngine("(a|b)*abb"); System.out.println(regexEngine.match("aabb")); // true System.out.println(regexEngine.match("ababb")); // true System.out.println(regexEngine.match("abab")); // false } } ``` 在上面的代码中,我们首先将输入的正则表达式转换为后缀表达式,然后使用栈来匹配输入字符串。在匹配过程中,我们使用了三种操作符:`|` 表示或,`*` 表示重复零次或多次,`+` 表示重复一次或多次。我们还处理了`.` 表示匹配任意字符的情况。 该实现并不完整,可能无法支持某些正则表达式语法。但是这可以作为一个简单的起点,以帮助您更好地理解正则表达式引擎的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值