求子集代码小结
相关问题
一.final int 和 static final int
1.final:final类型的成员变量的初始化,只能在声明时或在构造函数中完成赋值,且如果一个类中有多个构造函数,就要保证在每个类中都要完成对final类型变量的初始化。
(1)注意:final可以用在类、方法、变量上
- final用在类上,表明当前类不能被继承即没有子类
- final用在方法上,表明当前方法不能被override即不能被重写
- final用在变量上,表明当前变量是一个终态变量,是一个常量,一旦赋值后就不能被改变
(2)final初始化方法:
- 声明变量时直接赋值
- 在构造方法中完成赋值,如果一个类有过个构造方法,就要保证每个构造方法中都要完成对该final类型变量的初始化工作
2.static finial:此时的变量是一个静态变量,如果在类中,其还没有被实例化时,值就已经有了。所以对于一个static final类型的变量来说,只能在声明时就给它赋值
二.Stack
用法:
- 初始化:Stack<Integer> stack = new Stack<Integer>();
- 入栈:stack.push(数据);
- 出栈:stack.pop();
- 取栈顶元素:stack.peek();
- 判断栈是否为空:stack.empty;
三.String、StringBuilder、Stringbuffer三者间的区别
下段摘自:该段原著链接
1.String:
String类是不可变类,String对象一旦被创建,对象中的字符序列是不可被改变的
eg.
由下图可以看出在String类对象a已经被赋值为123后,对其再次进行赋值时,并不能对原来堆中实例对象
进行重新赋值,而是形成一个新的实例对象并指向“456”这个字符串,a则指向最新生成的实例对象,之前
的实例对象仍然存在,如果没有被再次引用,则会被垃圾回收
String a = "123";
a = "456";
// 打印出来的a为456
System.out.println(a);
2.StringBuffer:
StringBuffer对象则代表一个字符序列可被改变的字符串,当一个StringBuffer被创建以后,通过其提供的一系列
方法可以改变该字符串对象的字符序列。一旦通过StringBuffer生成了最终想要的字符串,就可以调用它的toString()
方法将其转化为一个String对象。
eg.
由下图所示,StringBuffer对象是一个字符序列可变的字符串,它没有重新生成一个对象,而且在原来的对象中可以
连接新的字符串。
StringBuffer b = new StringBuffer("123");
b.append("456");
// b打印结果为:123456
System.out.println(b);
3.StringBuilder:
StringBuilder类也可以代表可变字符串对象。实际上,StringBuilder和StringBuffer基本相似,两个类的构造
器个方法也基本相同。
4.StringBuffer类的相关方法:
- 初始化:StringBuffer 对象名 = new stringBuffer();
- 将指定的字符串追加到此字符序列:public StringBuffer append(String s)
- 将此字符序列用其反转形式取代:public StringBuffer reverse()
- 移除此序列的子字符串中的字符:public delete(int start, int end)
- 使用给定 String 中的字符替换此序列的子字符串中的字符:replace(int start, int end, String str)
- 返回此序列中数据的字符串表示形式:String toString() => 对象名.toString(); 即可返回String类的对象
- Capacity属性来设置对象的最大长度:StringBuilderMyStringBuilder = new StringBuilder("Hello World!", 25);
- 将字符串或对象添加到当前 StringBuilder中的指定位置:MyStringBuilder.Insert(6,"Beautiful ");
源代码:
package 算法设计;
import java.util.Stack;
public class 求子集 {
/** Set a value for target sum */
public static final int TARGET_SUM = 15;
private Stack<Integer> stack = new Stack<Integer>();
/** Store the sum of current elements stored in stack */
private int sumInStack = 0;
public void populateSubset(final int[] data, int fromIndex, int endIndex) {
if (sumInStack >= TARGET_SUM) {
if (sumInStack == TARGET_SUM) {
print(stack);
}
// there is no need to continue when we have an answer
// because nothing we add from here on in will make it
// add to anything less than what we have...
return;
}
for (int currentIndex = fromIndex; currentIndex < endIndex; currentIndex++) {
if (sumInStack + data[currentIndex] <= TARGET_SUM) {
stack.push(data[currentIndex]);
sumInStack += data[currentIndex];
/*
* Make the currentIndex +1, and then use recursion to proceed
* further.
*/
populateSubset(data, currentIndex + 1, endIndex);
sumInStack -= (Integer) stack.pop();
}
}
}
/**
* Print satisfied result. i.e. 15 = 4+6+5
*/
private void print(Stack<Integer> stack) {
StringBuilder sb = new StringBuilder();
// sb.append(TARGET_SUM).append(" = ");
for (Integer i : stack) {
sb.append(i).append(" ");
}
System.out.println(sb.deleteCharAt(sb.length() - 1).toString());
}
private static final int[] DATA = { 1, 3, 4, 5, 6, 2, 7, 8, 9, 10, 11, 13,
14, 15 };
public static void main(String[] args) {
求子集 example = new 求子集();
example.populateSubset(DATA, 0, DATA.length);
}
}