How to initialize a static SparseArray
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
How can I initialize a static
, unmodifiable instance of android.util.SparseArray
?
java android collections initialization sparse-array
add a comment |
How can I initialize a static
, unmodifiable instance of android.util.SparseArray
?
java android collections initialization sparse-array
1
You don't initialize static properties. What exactly are you trying to do?
– Eric
May 5 '13 at 23:01
1
@Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map
– firyice
May 5 '13 at 23:08
1
@Eric yes, exactly as I stated in this question, and the linked question/answer
– firyice
May 6 '13 at 0:30
You're better off using a HashMap for this purpose. I tried tackling the same problem.
– IgorGanapolsky
Dec 11 '15 at 16:00
add a comment |
How can I initialize a static
, unmodifiable instance of android.util.SparseArray
?
java android collections initialization sparse-array
How can I initialize a static
, unmodifiable instance of android.util.SparseArray
?
java android collections initialization sparse-array
java android collections initialization sparse-array
edited May 6 '13 at 1:46
Eric
56.7k18111128
56.7k18111128
asked May 5 '13 at 23:00
firyicefiryice
192422
192422
1
You don't initialize static properties. What exactly are you trying to do?
– Eric
May 5 '13 at 23:01
1
@Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map
– firyice
May 5 '13 at 23:08
1
@Eric yes, exactly as I stated in this question, and the linked question/answer
– firyice
May 6 '13 at 0:30
You're better off using a HashMap for this purpose. I tried tackling the same problem.
– IgorGanapolsky
Dec 11 '15 at 16:00
add a comment |
1
You don't initialize static properties. What exactly are you trying to do?
– Eric
May 5 '13 at 23:01
1
@Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map
– firyice
May 5 '13 at 23:08
1
@Eric yes, exactly as I stated in this question, and the linked question/answer
– firyice
May 6 '13 at 0:30
You're better off using a HashMap for this purpose. I tried tackling the same problem.
– IgorGanapolsky
Dec 11 '15 at 16:00
1
1
You don't initialize static properties. What exactly are you trying to do?
– Eric
May 5 '13 at 23:01
You don't initialize static properties. What exactly are you trying to do?
– Eric
May 5 '13 at 23:01
1
1
@Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map
– firyice
May 5 '13 at 23:08
@Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map
– firyice
May 5 '13 at 23:08
1
1
@Eric yes, exactly as I stated in this question, and the linked question/answer
– firyice
May 6 '13 at 0:30
@Eric yes, exactly as I stated in this question, and the linked question/answer
– firyice
May 6 '13 at 0:30
You're better off using a HashMap for this purpose. I tried tackling the same problem.
– IgorGanapolsky
Dec 11 '15 at 16:00
You're better off using a HashMap for this purpose. I tried tackling the same problem.
– IgorGanapolsky
Dec 11 '15 at 16:00
add a comment |
4 Answers
4
active
oldest
votes
You cannot do what you are attempting to. At least, not how you are attempting to do it. There is no implementation of SparseArray
that is unmodifiable.
However, you could create one. Here's how:
- Create a class, say
CustomSparseArray<E>
, and have it extendSparseArray
.
Override all methods that change the elements in the array, and replace them with something like this:
@Override
public void append(int key, E value) {
if (mLocked)
return; // Maybe throw an exception
super.append(key, value);
}
- Then, add in a member variable to the class,
boolean mLocked = false;
.
Next, you need a method like the following:
public void lock() { mLocked = true; }
Lastly, implement your static variable using a method similar to in the other post:
public class Test {
private static final CustomSparseArray<Integer> myArray;
static {
myArray = new CustomSparseArray<Integer>();
myArray.append(1, 1);
myArray.append(2, 5);
myArray.lock();
}
}
Then you have an unmodifiable SparseArray
in your static
variable myArray
.
This is exactly what I was looking for. Thank you!
– firyice
May 6 '13 at 20:29
add a comment |
Here is a better way using an anonymous class:
static final SparseIntArray myArray = new SparseIntArray() {
{
append(1, 2);
append(10, 20);
}
};
add a comment |
This works for me:
static final SparseIntArray CMyArray = new SparseIntArray();
static {
CMyArray.append(2, 4);
CMyArray.append(8, 3);
CMyArray.append(255, 1);
}
as per: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
add a comment |
For unmodifiable instance of android.util.SparseArray you can use Google implementation:
https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16389990%2fhow-to-initialize-a-static-sparsearray%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cannot do what you are attempting to. At least, not how you are attempting to do it. There is no implementation of SparseArray
that is unmodifiable.
However, you could create one. Here's how:
- Create a class, say
CustomSparseArray<E>
, and have it extendSparseArray
.
Override all methods that change the elements in the array, and replace them with something like this:
@Override
public void append(int key, E value) {
if (mLocked)
return; // Maybe throw an exception
super.append(key, value);
}
- Then, add in a member variable to the class,
boolean mLocked = false;
.
Next, you need a method like the following:
public void lock() { mLocked = true; }
Lastly, implement your static variable using a method similar to in the other post:
public class Test {
private static final CustomSparseArray<Integer> myArray;
static {
myArray = new CustomSparseArray<Integer>();
myArray.append(1, 1);
myArray.append(2, 5);
myArray.lock();
}
}
Then you have an unmodifiable SparseArray
in your static
variable myArray
.
This is exactly what I was looking for. Thank you!
– firyice
May 6 '13 at 20:29
add a comment |
You cannot do what you are attempting to. At least, not how you are attempting to do it. There is no implementation of SparseArray
that is unmodifiable.
However, you could create one. Here's how:
- Create a class, say
CustomSparseArray<E>
, and have it extendSparseArray
.
Override all methods that change the elements in the array, and replace them with something like this:
@Override
public void append(int key, E value) {
if (mLocked)
return; // Maybe throw an exception
super.append(key, value);
}
- Then, add in a member variable to the class,
boolean mLocked = false;
.
Next, you need a method like the following:
public void lock() { mLocked = true; }
Lastly, implement your static variable using a method similar to in the other post:
public class Test {
private static final CustomSparseArray<Integer> myArray;
static {
myArray = new CustomSparseArray<Integer>();
myArray.append(1, 1);
myArray.append(2, 5);
myArray.lock();
}
}
Then you have an unmodifiable SparseArray
in your static
variable myArray
.
This is exactly what I was looking for. Thank you!
– firyice
May 6 '13 at 20:29
add a comment |
You cannot do what you are attempting to. At least, not how you are attempting to do it. There is no implementation of SparseArray
that is unmodifiable.
However, you could create one. Here's how:
- Create a class, say
CustomSparseArray<E>
, and have it extendSparseArray
.
Override all methods that change the elements in the array, and replace them with something like this:
@Override
public void append(int key, E value) {
if (mLocked)
return; // Maybe throw an exception
super.append(key, value);
}
- Then, add in a member variable to the class,
boolean mLocked = false;
.
Next, you need a method like the following:
public void lock() { mLocked = true; }
Lastly, implement your static variable using a method similar to in the other post:
public class Test {
private static final CustomSparseArray<Integer> myArray;
static {
myArray = new CustomSparseArray<Integer>();
myArray.append(1, 1);
myArray.append(2, 5);
myArray.lock();
}
}
Then you have an unmodifiable SparseArray
in your static
variable myArray
.
You cannot do what you are attempting to. At least, not how you are attempting to do it. There is no implementation of SparseArray
that is unmodifiable.
However, you could create one. Here's how:
- Create a class, say
CustomSparseArray<E>
, and have it extendSparseArray
.
Override all methods that change the elements in the array, and replace them with something like this:
@Override
public void append(int key, E value) {
if (mLocked)
return; // Maybe throw an exception
super.append(key, value);
}
- Then, add in a member variable to the class,
boolean mLocked = false;
.
Next, you need a method like the following:
public void lock() { mLocked = true; }
Lastly, implement your static variable using a method similar to in the other post:
public class Test {
private static final CustomSparseArray<Integer> myArray;
static {
myArray = new CustomSparseArray<Integer>();
myArray.append(1, 1);
myArray.append(2, 5);
myArray.lock();
}
}
Then you have an unmodifiable SparseArray
in your static
variable myArray
.
edited May 23 '17 at 11:47
Community♦
11
11
answered May 6 '13 at 1:43
EricEric
56.7k18111128
56.7k18111128
This is exactly what I was looking for. Thank you!
– firyice
May 6 '13 at 20:29
add a comment |
This is exactly what I was looking for. Thank you!
– firyice
May 6 '13 at 20:29
This is exactly what I was looking for. Thank you!
– firyice
May 6 '13 at 20:29
This is exactly what I was looking for. Thank you!
– firyice
May 6 '13 at 20:29
add a comment |
Here is a better way using an anonymous class:
static final SparseIntArray myArray = new SparseIntArray() {
{
append(1, 2);
append(10, 20);
}
};
add a comment |
Here is a better way using an anonymous class:
static final SparseIntArray myArray = new SparseIntArray() {
{
append(1, 2);
append(10, 20);
}
};
add a comment |
Here is a better way using an anonymous class:
static final SparseIntArray myArray = new SparseIntArray() {
{
append(1, 2);
append(10, 20);
}
};
Here is a better way using an anonymous class:
static final SparseIntArray myArray = new SparseIntArray() {
{
append(1, 2);
append(10, 20);
}
};
edited Dec 26 '17 at 9:16
Hi I'm Frogatto
20.5k86097
20.5k86097
answered Dec 19 '13 at 2:43
PeterPeter
4,38294393
4,38294393
add a comment |
add a comment |
This works for me:
static final SparseIntArray CMyArray = new SparseIntArray();
static {
CMyArray.append(2, 4);
CMyArray.append(8, 3);
CMyArray.append(255, 1);
}
as per: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
add a comment |
This works for me:
static final SparseIntArray CMyArray = new SparseIntArray();
static {
CMyArray.append(2, 4);
CMyArray.append(8, 3);
CMyArray.append(255, 1);
}
as per: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
add a comment |
This works for me:
static final SparseIntArray CMyArray = new SparseIntArray();
static {
CMyArray.append(2, 4);
CMyArray.append(8, 3);
CMyArray.append(255, 1);
}
as per: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
This works for me:
static final SparseIntArray CMyArray = new SparseIntArray();
static {
CMyArray.append(2, 4);
CMyArray.append(8, 3);
CMyArray.append(255, 1);
}
as per: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
answered Mar 6 '17 at 9:23
PJ_FinneganPJ_Finnegan
6931811
6931811
add a comment |
add a comment |
For unmodifiable instance of android.util.SparseArray you can use Google implementation:
https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java
add a comment |
For unmodifiable instance of android.util.SparseArray you can use Google implementation:
https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java
add a comment |
For unmodifiable instance of android.util.SparseArray you can use Google implementation:
https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java
For unmodifiable instance of android.util.SparseArray you can use Google implementation:
https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java
answered Jan 3 at 9:28
intraintra
34036
34036
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16389990%2fhow-to-initialize-a-static-sparsearray%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You don't initialize static properties. What exactly are you trying to do?
– Eric
May 5 '13 at 23:01
1
@Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map
– firyice
May 5 '13 at 23:08
1
@Eric yes, exactly as I stated in this question, and the linked question/answer
– firyice
May 6 '13 at 0:30
You're better off using a HashMap for this purpose. I tried tackling the same problem.
– IgorGanapolsky
Dec 11 '15 at 16:00