Java Code Examples for java.util.Stack#toArray()

The following examples show how to use java.util.Stack#toArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Solution.java    From LeetCode-Solution-in-Good-Style with Apache License 2.0 6 votes vote down vote up
public int[][] merge(int[][] intervals) {
    int len = intervals.length;
    if (len == 0) {
        return new int[0][0];
    }

    // 按照起点排序
    Arrays.sort(intervals, Comparator.comparingInt(i -> i[0]));

    Stack<int[]> stack = new Stack<>();
    stack.push(intervals[0]);
    for (int i = 1; i < len; i++) {
        if (stack.peek()[1] < intervals[i][0]) {
            stack.push(intervals[i]);
        } else {
            // 更新
            stack.peek()[1] = Math.max(stack.peek()[1], intervals[i][1]);
        }
    }
    return stack.toArray(new int[0][]);
}
 
Example 2
Source File: Solution.java    From LeetCode-Solution-in-Good-Style with Apache License 2.0 6 votes vote down vote up
public int[][] merge(List<Interval> intervals) {
    int len = intervals.size();
    if (len < 2) {
        return new int[0][0];
    }

    intervals.sort(Comparator.comparingInt((Interval a) -> a.start));

    // 因为每次我们都拿最后一个元素,因此用栈是比较方便的
    Stack<Interval> stack = new Stack<>();
    stack.push(intervals.get(0));
    for (int i = 1; i < len; i++) {
        Interval curInterval = intervals.get(i);
        Interval peek = stack.peek();
        if (curInterval.start > peek.end) {
            stack.add(curInterval);
        } else {
            // 注意,这里应该取最大
            peek.end = Math.max(curInterval.end, peek.end);
        }
    }
    return stack.toArray(new int[0][]);
}
 
Example 3
Source File: DefaultStringArrayReader.java    From domino-jackson with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public String[] readArray(JsonReader reader) {
    Stack<String> stringStack = new Stack<>();
    reader.beginArray();
    while (JsonToken.END_ARRAY != reader.peek()) {
        if (JsonToken.NULL == reader.peek()) {
            reader.skipValue();
            stringStack.push(null);
        } else {
            stringStack.push(reader.nextString());
        }
    }
    reader.endArray();

    return stringStack.toArray(new String[stringStack.size()]);
}
 
Example 4
Source File: AlloppNode.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static String subtreeAsText(AlloppNode node, String s, Stack<Integer> x, int depth, String b) {
    Integer[] y = x.toArray(new Integer[x.size()]);
    StringBuffer indent = new StringBuffer();
    for (int i = 0; i < depth; i++) {
        indent.append("  ");
    }
    for (int i = 0; i < y.length; i++) {
        indent.replace(2*y[i], 2*y[i]+1, "|");
    }
    if (b.length() > 0) {
        indent.replace(indent.length()-b.length(), indent.length(), b);
    }
    s += indent;
    s += node.asText(indent.length());
    s += System.getProperty("line.separator");
    String subs = "";
    if (node.nofChildren() > 0) {
        x.push(depth);
        subs += subtreeAsText(node.getChild(0), "", x, depth+1, "-");
        x.pop();
        subs += subtreeAsText(node.getChild(1), "", x, depth+1, "`-");
    }
    return s + subs;
}
 
Example 5
Source File: AlloppSpeciesBindings.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
private String subtreeAsText(GeneUnionNode node, String s, Stack<Integer> x, int depth, String b) {
    Integer[] y = x.toArray(new Integer[x.size()]);
    StringBuffer indent = new StringBuffer();
    for (int i = 0; i < depth; i++) {
        indent.append("  ");
    }
    for (int i = 0; i < y.length; i++) {
        indent.replace(2*y[i], 2*y[i]+1, "|");
    }
    if (b.length() > 0) {
        indent.replace(indent.length()-b.length(), indent.length(), b);
    }
    s += indent;
    s += node.asText(indent.length());
    s += System.getProperty("line.separator");
    String subs = "";
    if (node.child.length > 0) {
        x.push(depth);
        subs += subtreeAsText(node.child[0], "", x, depth+1, "-");
        x.pop();
        subs += subtreeAsText(node.child[1], "", x, depth+1, "`-");
    }
    return s + subs;
}
 
Example 6
Source File: VersionFactory.java    From AMIDST with GNU General Public License v3.0 6 votes vote down vote up
public void scanForLocalVersions() {
	File versionPath = new File(Util.minecraftDirectory + "/versions");
	if (!versionPath.exists()) {
		Log.e("Cannot find version directory.");
		return;
	} else if (!versionPath.isDirectory()) {
		Log.e("Attempted to open version directory but found file.");
		return;
	}
	File[] versionDirectories = versionPath.listFiles();
	Stack<MinecraftVersion> versionStack = new Stack<MinecraftVersion>();
	for (int i = 0; i < versionDirectories.length; i++) {
		MinecraftVersion version = MinecraftVersion.fromVersionPath(versionDirectories[i]);
		if (version != null)
			versionStack.add(version);
	}
	if (versionStack.size() == 0)
		return;
	
	localVersions = new MinecraftVersion[versionStack.size()];
	versionStack.toArray(localVersions);
}
 
Example 7
Source File: Region.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Collects all outline segments of the internal {@link Rectangle}s.
 *
 * @return all the outline segments of the internal {@link Rectangle}s
 */
@Override
protected Line[] getAllEdges() {
	Stack<Line> edges = new Stack<>();

	for (Rectangle r : rects) {
		for (Line e : r.getOutlineSegments()) {
			edges.push(e);
		}
	}
	return edges.toArray(new Line[] {});
}
 
Example 8
Source File: Ring.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Line[] getAllEdges() {
	Stack<Line> edges = new Stack<>();

	for (Polygon t : triangles) {
		for (Line e : t.getOutlineSegments()) {
			edges.push(e);
		}
	}
	return edges.toArray(new Line[] {});
}
 
Example 9
Source File: RandomCreditCardNumberGenerator.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private static String[] credit_card_number( String[] prefixList, int length, int howMany ) {

    Stack<String> result = new Stack<String>();
    for ( int i = 0; i < howMany; i++ ) {
      int randomArrayIndex = (int) Math.floor( Math.random() * prefixList.length );
      String ccnumber = prefixList[randomArrayIndex];
      result.push( completed_number( ccnumber, length ) );
    }

    return result.toArray( new String[result.size()] );
  }