Navigating an ArrayList - java

i have a problem on my implementation of QM Method on java.
I am having a problem with navigating a 2D ArrayList. My goal in the code is by having the first element of ArrayList in being the address and each time another binary is paired up, i will find that address and add the address of the paired binary.
Is there any way to use the indexOf Function in finding the index of the first element in a 2D Array?
Example.
My 2D ArrayList named table is
[4][3][7];[7][4];[8][3];[9];
I want to add int 5 to the the line where [4] is. so my code goes table.get(table.indexOf(4)).add(5) but it keeps saying that the index is -1. Please help thanks

So consider...
ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
// populate list
Integer index = myList.indexOf(4);
// index is -1 because 4 is not of type: ArrayList<Integer>
If you need to add values to an ArrayList based on an integer index, I would consider switching to a different container type such as:
HashMap<Integer, ArrayList<Integer>>
The alternative is not using indexOf ... but instead write your own function to loop through 0 to myList.size() looking for myList.get(loopIndex).get(0) = address to be found, then returning the outer ArrayList index where it was found

Related

How to find last non-empty index in an array

I am using an input file to populate my array with different strings. how do I check up to where the array is filled? I initialize the size of my array to 30000, but how do I check up to which index it contains a string?
Thank you for your help!
Low-level approach: while populating the array, maintain a count variable, adding 1 to it on every element you insert into the array.
Professional approach: instead of an array, use a java.util.List, e.g. a java.util.ArrayList. List instances do the counting for you (and by the way, don't waste 25000 empty elements if your file only contains 5000).

How can I sort a 2D array and keep track of the original indexes in Java

I have a 2D array called distance[][]
It holds my values in column 0:
Example:
{ 5
7
3
9 }
It seems like a 1D array, however I've converted it into a 2D array in order to be able to keep track of my indexes.
The way I access my first value is distance[0][0].
My question is: how can I keep track of the original index positions if I were to sort my array. I was under the impression that I may need to create a corresponding list of numbers in ascending order which correspond with the position my of distance values.
What would I do after that?
All you need is to use comparator. Consider below code -
Arrays.sort(arr, new java.util.Comparator<int[]>(){
public int compare(int[] a,int[] b){
return a[0]-b[0];
}
});
arr is your orginial 2d array, where first column contains the number to sorted and second column contains index
The simplest solution would be to create a Pair class that holds the item and its corresponding index.Then, keep a list of this Pair classes int two different lists and than sort one of them.Since in java, you can use references without creating a copy object,this should be fairly easy.Now the sorted list will contain the values and its corresponding indices sorted according to the value and the unsorted list will have the Pair sin the order you entered them, that is ,sorted according to the index.

ArrayList/LinkedList get First Index number

Is is possible to get out of an ArrayList the first number of the first index?
Here's an Example:
In there are 5 items:
path = {0.5,5.0},{0.6,6.0},{0.7,7.0},{0.8,8.0},{0.9,9.0}
And I want to get the number 5.0 out of {0.5,5.0}...
I tried it with path.get(0) But it only gives me {0.5,5.0} back.
Is it possible to get 5.0 out of it without getting all the other numbers?
If your ArrayList contains arrays, this is the way to go
myList.get(0)[1] // You're getting the index 1 from the 1st array of your ArrayList
Otherwise, if it is containing other ArrayList's
myList.get(0).get(1) // Same logic as above applied to the Collection
If your curly braces in the ArrayList are actually brackets (they probably are), you can use:
myArray.get(0)[index] to get the index you want. In your example it is:
myArray.get(0)[1];
Note: if your ArrayList elements are also ArrayList then you need to use get(0).get(1) instead of get(0)[1].

Java Finding index of an element in a java list

Simple question, but everytime I google it there are only examples using an ArrayList, and list.indexOf(3), doesn't work on int[] nums = new int[10]. Soo im a little confused.
Here's the real problem. You appear to think that
int[] nums = new int[10]
is defining a List or a list. It isn't. It is an array. The words "list" and "array" are NOT synonymous in the context of Java. So when you Google for "finding an index of an element in a list" ... you don't get any useful hits.
If you use the >>correct<< terms when searching (Google, SO search, anything) you are far more likely to get useful search results.
For example:
How to find index of int array in Java from a given value?
FWIW, if you want a (real) list of integers in Java you declare it like this:
List<Integer> nums = new ArrayList<>();
Notes
You have to use Integer as the type parameter, not int.
You don't need to provide 10 as a parameter. And if you do, it is a hint that tells the ArrayList what the initial capacity of the list should be. (For an array it is the actual size ... and it can't be changed).
If you want to use indexOf(3) on an array you could create a List from it
ArrayList<Integer> myInts = new ArrayList<Integer>(Arrays.asList(array))
and then you can use
myInts.indexOf(3)

Remove element from Array Java

I am currently making a tiny little program that should be able to select a random element i put into the array using a Random of course (For practice purposes) and when a element in the array has been chosen at random. i want to remove this element in the array, so how do you remove a element in a array the easiest way?
Its the only thing i want to know. I got everything else sorted. It's just removing the element it has chosen (the random takes a random number between 0 and the amount of elements in the array, so if it chooses 0, it will take the first element in the array, and so on)
You can use ArrayList which support remove or add function, which actually is an resizable array.
You can't remove an element from an array. You can replace it with some other value that indicates "nothing", null for example.
An easy solution is to convert the array into a list.
list = Arrays.asList(array);
Remove any element from the list and then revert it back to an array using
array = list.toArray();
Hope it helps.
Use List instead of Array, and if you want to stay on Array than there is 2 solution,
Create another Array ignoring your element which you want to delete.
create a List using Array.asList(...) than remove element from list and convert back to Array.
but according to me its better for you as well as java to use List. because List provide a many build-in functions.

Categories

Resources