How to declare an imageIcon array? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am somewhat new to Java, so excuse this really dumb question. I was just wondering, how does one declare a 2D array of ImageIcons?

Say like this:
ImageIcon[][] arr = new ImageIcon[10][5];
Note that after this line, the array elements will be uninitialized (they will be all equal to null).
If you want to initialize them, you need to loop through your array and call some of the ImageIcon constructors e.g.
arr[i][j] = new ImageIcon();

Related

how to nest if statements in HashSet? [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to find the lentgh of a set from a preference.
I tired set.length, but it doesnt work. Anyone know another way to find a length of a set?
set = prefs.getStringSet("key", null);
int X = set.length; //it doesn't like length....
I believe instead of set.length, you'll want to call set.size()
Your set is a Set<String>, not a String[]. So you have to call:
int x = set.size();

Converting a float to the integer with the same bit representation as it, in java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I think this is not hard in C with pointers. However, how would one do this in java? EG a float is represented as 101001, and you want the integer represented by 101001 (obviously shortened since they're really 32 bits)? Just curious.
I think you want Float.floatToIntBits().

If I add a object into an ArrayList at index 1,000,000, will the ArrayList consume a lot of memory? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Say the ArrayList is empty before. Then I add one object to the ArrayList at index 1,000,000.
Will the ArrayList create 1,000,001 pointers or just create one pointer?
An IndexOutOfBoundsException will be thrown:
IndexOutOfBoundsException - if the index is out of range (index < 0 ||
index > size())
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(int, E)
Since you ask, you haven't even tried to run it. You will get a IndexOutOfBounds exception.

Assign information to element in array in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is it possible to assign information to an element in an array in java?
For example, can I have an element containing a title and a number etc?
How do you go about this? thanks
Yes, instead of having an array of primitive types (like ints) or Strings, define your own class and then create an array of that. Then you can have whatever information you want in there.
Alternatively, if the array must be an array of primitives or Strings because some other method requires it to be in that form, you could either create a second array like that on demand, or use a second array for the associated data (again you might create your own class for this).

Does Collections.sort() shuffles equal priority elements? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Or just let them alone or throws an exception?
If the third one, how can I sort elements in ArrayList saving the old order of priority equal elements?
Thanks in advance.
From the documentation:
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
To save the order of the old arraylist, simply make a copy of the arraylist ahead of time. You can do this by saying:
List<Integer> oldListOrder = new ArrayList<Integer>(listToBeSorted);
Collections.sort(listToBeSorted);
Keep in mind the elements will reference the same objects in both lists. That may not affect you, it depends on what you do next.

Categories

Resources