Skip to content
Merged

L5 #3

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions group13/2729382520/L5/datastructure/less5/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.github.vxzh.datastructure.less5;

import java.util.ArrayList;

public class Stack {

private ArrayList elementData;

public Stack() {
this.elementData = new ArrayList();
}

public int size() {
return elementData.size();
}

public boolean isEmpty() {
return elementData.size() == 0;
}

public void push(Object o) {
elementData.add(o);
}

public Object pop() {
if (isEmpty())
throw new RuntimeException("EmptyStackException");
Object obj = peek();
elementData.remove(elementData.size() - 1);
return obj;
}

public Object peek() {
if (isEmpty())
throw new RuntimeException("EmptyStackException");
return elementData.get(elementData.size() - 1);
}

}
120 changes: 120 additions & 0 deletions group13/2729382520/L5/datastructure/less5/StackUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.github.vxzh.datastructure.less5;


public class StackUtil {

public static void main(String[] args) {
String s = "(9[O{er}P]0)";
System.out.println(isValidPairs(s));
}

/**
* 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*/
public static void reverse(Stack s) {
Stack stack = s;
s = new Stack();
while (!stack.isEmpty()) {
s.push(stack.pop());
}
}

/**
* 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*
* @param o
*/
public static void remove(Stack s, Object o) {
Stack stack = s;
s = new Stack();
while (!stack.isEmpty()) {
Object obj = stack.pop();
if (obj != o) {
s.push(obj);
}
}

}

/**
* 从栈顶取得len个元素, 原来的栈中元素保持不变
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*
* @param len
* @return
*/
public static Object[] getTop(Stack s, int len) {
if (len > s.size()) {
Object[] arr = new Object[s.size()];
int i = 0;
while (!s.isEmpty()) {
arr[i] = s.pop();
i++;
}
for (int j = 0; j < arr.length; j++) {
s.push(arr[j]);
}
return arr;
} else {
Object[] arr = new Object[len];
int i = 0;
while (!s.isEmpty()) {
arr[i] = s.pop();
i++;
}
for (int j = 0; j < arr.length; j++) {
s.push(arr[j]);
}
return arr;
}
}

/**
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz
* 使用堆栈检查字符串s中的括号是不是成对出现的。
* 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true
* 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false;
*
* @param s
* @return
*/
public static boolean isValidPairs(String s) {
char[] arr = s.toCharArray();
Stack st1 = new Stack();
Stack st2 = new Stack();
for (int i = 0; i < arr.length; i++) {
st1.push(arr[i]);
}
for (int i = arr.length - 1; i >= 0; i--) {
st2.push(arr[i]);
}

while (!st1.isEmpty()) {

char ch1 = (char) st1.pop();
char ch2 = (char) st2.pop();
switch (ch1) {
case '{':
if (ch2 != '}') {
return false;
}
break;
case '[':
if (ch2 != ']') {
return false;
}
break;
case '(':
if (ch2 != ')') {
return false;
}
break;
}
}

return true;
}


}
26 changes: 26 additions & 0 deletions group13/2729382520/L5/jvm/clz/AccessFlag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.vxzh.jvm.clz;

public class AccessFlag {
private int flagValue;

public AccessFlag(int value) {
this.flagValue = value;
}

public int getFlagValue() {
return flagValue;
}

public void setFlagValue(int flag) {
this.flagValue = flag;
}

public boolean isPublicClass() {
return (this.flagValue & 0x0001) != 0;
}

public boolean isFinalClass() {
return (this.flagValue & 0x0010) != 0;
}

}
80 changes: 80 additions & 0 deletions group13/2729382520/L5/jvm/clz/ClassFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.vxzh.jvm.clz;

import io.github.vxzh.jvm.constant.ClassInfo;

public class ClassFile {

private int minorVersion;
private int majorVersion;

private AccessFlag accessFlag;
private ClassIndex clzIndex;
private ConstantPool pool;


public ClassIndex getClzIndex() {
return clzIndex;
}

public AccessFlag getAccessFlag() {
return accessFlag;
}

public void setAccessFlag(AccessFlag accessFlag) {
this.accessFlag = accessFlag;
}


public ConstantPool getConstantPool() {
return pool;
}

public int getMinorVersion() {
return minorVersion;
}

public void setMinorVersion(int minorVersion) {
this.minorVersion = minorVersion;
}

public int getMajorVersion() {
return majorVersion;
}

public void setMajorVersion(int majorVersion) {
this.majorVersion = majorVersion;
}

public void setConstPool(ConstantPool pool) {
this.pool = pool;

}

public void setClassIndex(ClassIndex clzIndex) {
this.clzIndex = clzIndex;
}


public void print() {

if (this.accessFlag.isPublicClass()) {
System.out.println("Access flag : public ");
}
System.out.println("Class Name:" + getClassName());

System.out.println("Super Class Name:" + getSuperClassName());


}

private String getClassName() {
int thisClassIndex = this.clzIndex.getThisClassIndex();
ClassInfo thisClass = (ClassInfo) this.getConstantPool().getConstantInfo(thisClassIndex);
return thisClass.getClassName();
}

private String getSuperClassName() {
ClassInfo superClass = (ClassInfo) this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex());
return superClass.getClassName();
}
}
22 changes: 22 additions & 0 deletions group13/2729382520/L5/jvm/clz/ClassIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.vxzh.jvm.clz;

public class ClassIndex {
private int thisClassIndex;
private int superClassIndex;

public int getThisClassIndex() {
return thisClassIndex;
}

public void setThisClassIndex(int thisClassIndex) {
this.thisClassIndex = thisClassIndex;
}

public int getSuperClassIndex() {
return superClassIndex;
}

public void setSuperClassIndex(int superClassIndex) {
this.superClassIndex = superClassIndex;
}
}
35 changes: 35 additions & 0 deletions group13/2729382520/L5/jvm/clz/ConstantPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.vxzh.jvm.clz;

import io.github.vxzh.jvm.constant.ConstantInfo;
import io.github.vxzh.jvm.constant.UTF8Info;

import java.util.ArrayList;
import java.util.List;

public class ConstantPool {

private List<ConstantInfo> constantInfos = new ArrayList<ConstantInfo>();


public ConstantPool() {

}

public void addConstantInfo(ConstantInfo info) {

this.constantInfos.add(info);

}

public ConstantInfo getConstantInfo(int index) {
return this.constantInfos.get(index);
}

public String getUTF8String(int index) {
return ((UTF8Info) this.constantInfos.get(index)).getValue();
}

public Object getSize() {
return this.constantInfos.size() - 1;
}
}
30 changes: 30 additions & 0 deletions group13/2729382520/L5/jvm/constant/ClassInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.vxzh.jvm.constant;

import io.github.vxzh.jvm.clz.ConstantPool;

public class ClassInfo extends ConstantInfo {
private int tag = ConstantInfo.CONSTANT_CLASS_INFO;
private int nameIndex;

public ClassInfo(ConstantPool pool) {
super(pool);
}

public int getNameIndex() {
return nameIndex;
}

public void setNameIndex(int nameIndex) {
this.nameIndex = nameIndex;
}

public int getTag() {
return tag;
}

public String getClassName() {
int index = getNameIndex();
UTF8Info utf8Info = (UTF8Info) constantPool.getConstantInfo(index);
return utf8Info.getValue();
}
}
Loading