Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.github.wdn.coding2017.basic.stack;

import com.github.wdn.coding2017.basic.Stack;

public class StackUtil {


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

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

/**
* 从栈顶取得len个元素, 原来的栈中元素保持不变
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
* @param len
* @return
*/
public static Object[] getTop(Stack s,int len) {
if (len > s.size() || len < 0) {
throw new IndexOutOfBoundsException();
}
Object[] result = new Object[len];
Stack stack = new Stack();
for (int i = 0; i < len; i++) {
Object o = s.pop();
result[i]=o;
stack.push(o);
}
while (!stack.isEmpty()){
s.push(stack.pop());
}
return result;
}
/**
* 字符串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){
if(s.length()<2){
return false;
}
Stack s1 = new Stack();
Stack s2 = new Stack();
char[] chars = s.toCharArray();
int charsLength = chars.length;
if(charsLength%2==1 && isBrackets(chars[charsLength / 2])){
return false;
}
for (int i = 0; i < charsLength/2; i++) {
char c = chars[i];
if (isBrackets(c)) {
s1.push(c);
}
}
for (int i = charsLength-1; i > charsLength/2; i--) {
char c = chars[i];
if (isBrackets(c)) {
s2.push(c);
}
}
if (s1.size() != s2.size()) {
return false;
}
for (int i = 0; i < s1.size(); i++) {
if (!isPairing((Character) s1.pop(), (Character) s2.pop())) {
return false;
}
}
return true;
}
// parenthesis 圆括号
// square brackets 方括号
// braces 大括号
// 这里用bracket表示统称
private static boolean isBrackets(char c){
if('['==c||']'==c||
'('==c||')'==c||
'{'==c||'}'==c){
return true;
}
return false;
}

private static boolean isPairing(char left, char right) {
if(left=='(' && right==')'){
return true;
}else if(left=='[' && right==']'){
return true;
}else if(left=='{' && right=='}'){
return true;
}else {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.wdn.coding2017.basic;

import com.github.wdn.coding2017.basic.stack.StackUtil;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class StackUtilTest {

@Test
public void testReverse(){
Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
StackUtil.reverse(stack);
while (!stack.isEmpty()){
System.out.println(stack.pop());
}
}
@Test
public void testRemove(){
Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
StackUtil.remove(stack,4);
while (!stack.isEmpty()){
System.out.println(stack.pop());
}
}
@Test
public void testGetTop(){
Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
Object[] o = StackUtil.getTop(stack,0);
System.out.println(Arrays.toString(o));
while (!stack.isEmpty()){
System.out.println(stack.pop());
}
}
@Test
public void testIsValidPairs(){
Assert.assertEquals(true,StackUtil.isValidPairs("([e{d}f])"));
Assert.assertEquals(false,StackUtil.isValidPairs("([b{x]y})"));
Assert.assertEquals(false,StackUtil.isValidPairs("([({e}f])"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.wdn.coding2017.jvm.clz;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class AccessFlag {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.wdn.coding2017.jvm.clz;

import com.github.wdn.coding2017.jvm.constant.ConstantPool;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class ClassFile {
private int minorVersion;
private int majorVersion;
private ConstantPool constantPool;
private AccessFlag accessFlag;
private ClassIndex classIndex;
public void print() {
}

public int getMinorVersion() {
return minorVersion;
}

public int getMajorVersion() {
return majorVersion;
}

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

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

public ConstantPool getConstantPool() {
return constantPool;
}

public void setConstantPool(ConstantPool constantPool) {
this.constantPool = constantPool;
}

public AccessFlag getAccessFlag() {
return accessFlag;
}

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

public ClassIndex getClassIndex() {
return classIndex;
}

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

public ClassIndex getClzIndex() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.wdn.coding2017.jvm.clz;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class ClassIndex {
private int thisClassIndex;
private int superClassIndex;
public ClassIndex(int thisClassIndex,int superClassIndex){
this.thisClassIndex = thisClassIndex;
this.superClassIndex = superClassIndex;
}
public int getThisClassIndex() {
return thisClassIndex;
}

public int getSuperClassIndex() {
return superClassIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class ClassInfo extends ConstantInfo{
private int nameIndex;
public ClassInfo(ConstantPool constantPool){
super(constantPool);
}
@Override
public int getType() {
return CLASS_INFO;
}
@Override
public String getValue() {
return getConstantPool().getConstantInfo(nameIndex).getValue();
}

public int getNameIndex() {
return nameIndex;
}

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

public int getUtf8Index() {
return 0;
}

public String getClassName() {
return getConstantPool().getConstantInfo(nameIndex).getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public abstract class ConstantInfo {
public static final int UTF8_INFO = 1;
public static final int FLOAT_INFO = 4;
public static final int CLASS_INFO = 7;
public static final int STRING_INFO = 8;
public static final int FIELD_INFO = 9;
public static final int METHOD_INFO = 10;
public static final int NAME_AND_TYPE_INFO = 12;
protected ConstantPool constantPool;
public ConstantInfo(){

}
public ConstantInfo(ConstantPool constantPool){
this.constantPool = constantPool;
}
public abstract int getType();
public abstract String getValue();
public ConstantPool getConstantPool(){
return constantPool;
}
public ConstantInfo getConstantInfo(int index){
return constantPool.getConstantInfo(index);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.wdn.coding2017.jvm.constant;


import java.util.ArrayList;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class ConstantPool {
public static ArrayList<ConstantInfo> constantPool = new ArrayList<ConstantInfo>();
static{
constantPool.add(new NullConstantInfo());
}
public void put(ConstantInfo info){
constantPool.add(info);
}
public int getSize() {
return constantPool.size()-1;
}

public ConstantInfo getConstantInfo(int i) {
return constantPool.get(i);
}
@Override
public String toString(){
StringBuffer stringBuffer = new StringBuffer();
for (int i = 1; i < constantPool.size(); i++) {
stringBuffer.append("#"+i+"=>"+constantPool.get(i).getValue());
}
return stringBuffer.toString();
}
}
Loading