How do i get the number of selected elements from my jlist? So that i can create an int[] array and add the selected indices to this array.
JList.getSelectedIndices().length would give you the array length :) Refer Javadocs mate
Related
I'm trying to add a string into my array in the 3rd position because I need to do so for a loop I'm executing after it.
ArrayList<String> namesArray = new ArrayList<>();
namesArray.add(3, mString);
It gives an out of bonds expection, is this not possible to do?
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 0
When you say
arrayList.add(n, value);
the result is that after the add, arrayList.get(n) will have that value. You're trying to arrange things so that arrayList.get(3) will be mString. However, an ArrayList must be a list of consecutive elements; it can't be a "sparse array". That is, for arrayList.get(3) to exist, arrayList.get(0), arrayList.get(1), and arrayList.get(2) must also exist.
I don't know what you want those values to be (maybe null?), but you do have to set them. Java's ArrayList doesn't have an add method that automatically fills in the gap with a default value. You can add 3 nulls to the array like this:
arrayList.addAll(Arrays.asList(new String[3]));
You should fill the array with empty string before index 3 like this:
namesArray.add("");
namesArray.add("");
namesArray.add("");
Then you won't get that exception
I am trying to push a selected value at index 'a' to index 'a+1'.
But if index 'a+1' is not empty, I have to swap the two values. For this, I do not know how to obtain the value at index 'a+1'. It is not selected. The JList API does not support random access.
How do I implement this ?
Thanks.
To access the elements in a JList you need to get hold of its ListModel.
The model has a getElementAt(int) method.
item = myJList.getModel().getElementAt(a+1);
How do I determine which column in a double ArrayList has the same value in all rows? My ArrayList looks like the image below.
Update: Seems like there are many where were confused by the question. I should have asked in a more eloquent way.
I have a two dimensional ArrayList which consists of ArrayLists. I have a certain number of rows and columns in this two dimensional ArrayList. I would like to create a method that would return how many numbers of columns are in that ArrayList. That was what I meant in my original question. Now I know how to create this method as I got help from some of the answers below.
I suppose you have an ArrayList of ArrayLists, then you can do this to retrieve number of "columns":
list.get(0).size()
It means get the first ArrayList from list and get it's size.
Assuming that you just have nested ArrayLists, and the number of rows is represented by the "top" ArrayList size, you can just get the size of the first nested one:
aList.get(0).size();
...assuming of course that aList is your top most ArrayList.
Do you mean you have a
List<List<MyType>> listOfList = ...
and you want to know the size of the inner dimension?
int width = listOfList.get(0).size();
assuming all lists/rows are the same size/width.
I would like code to store an unknown number of integer values from a website's drop-down list into an array and then compare the values in an existing array with the array values retrieved from the drop-down list.
I was thinking maybe a For loop could work? I apologize but I don't have any sample code to put up because I don't know how to start with creating this code. I do have Basic Java knowledge
You can use arraylists, they are dynamic and don't have to be specified size in advance unlike arrays.
List<Integer> integers = new ArrayList<Integer>();
You can add to integers by integers.add();
I don't understand how to decrease the length of a String array. For example, with this code:
String[][] array = new String[5][2];
array[1][0] = "what";
array[2][0] = "is";
.....
.....
array[5][0] = "?";
How can I delete array[5][0] and get array.length to be 4, not 5?
If you want to remove the array element from the end , you can also use Arrays.copyOf() since jdk 1.6+
For example:
array = Arrays.copyOf(array, 4);
It just copy the original array 's first 4 elements to a new array , so it have the same effect as deleting the array[5]
If you want to remove an element from an specified index , you can use ArrayUtils.remove() from Apache Commons Lang 3 to do it .
/**Remove the element at index 3**/
array =ArrayUtils.remove(array, 3);
You cannot delete an item from an array.
But you can create a new array with smaller size and copy the content of the old array to the new one. Then, assign the value of the reference to the new array.
Consider using java.util.List. It has a method remove().
Using arrays for such things is very time consuming. I can suggest these solutions:
1. Use one of the data structures provided by Java libraries. I'd go with HashMap since its structure allows mapping a value to a key (HashMap ) and it does the part of adding, finding and removing items. You can also use them for multi-level hashmaps if you need more than 2 columns (HashMap ) you can look that up.
2. Use a List or ArrayList structure. Make a list that contains arrays or a special structure you create to store your data.
3. (Not recommended) Go for the manual route. If you have a fixed-length array, you can shift the rows back to remove that row, and use an index to define the last row. If you have a dynamic-length array you'll need to reconstruct it each time you remove a row.