From fd37cf1d0d3a37ade0e74f07e91c97a925ba42db Mon Sep 17 00:00:00 2001 From: guptasourabh Date: Mon, 13 Mar 2017 08:29:41 +0530 Subject: [PATCH] JsonArray with capacity in constructor (#1033) JsonArray with capacity in constructor --- gson/src/main/java/com/google/gson/JsonArray.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonArray.java b/gson/src/main/java/com/google/gson/JsonArray.java index 19299d14..96fe23ed 100644 --- a/gson/src/main/java/com/google/gson/JsonArray.java +++ b/gson/src/main/java/com/google/gson/JsonArray.java @@ -39,14 +39,21 @@ public final class JsonArray extends JsonElement implements Iterable(); } + + public JsonArray(int capacity) { + elements = new ArrayList(capacity); + } @Override JsonArray deepCopy() { - JsonArray result = new JsonArray(); - for (JsonElement element : elements) { - result.add(element.deepCopy()); + if (!elements.isEmpty()) { + JsonArray result = new JsonArray(elements.size()); + for (JsonElement element : elements) { + result.add(element.deepCopy()); + } + return result; } - return result; + return new JsonArray(); } /**