I just looked up array and arrayList
and found out that an array is fixed length and can't be changed while an arraylist can be changed and is variable in length
my question is:
is array == tuple in python?
and is arraylist == list in python?
and if they aren't what are array and arraylist's python equivalent?
ArrayList in java and list in python are both dynamic arrays. They both have O(1) average indexing time and O(1) average adding an element to the end time.
Array in java is not tuple in python. While it is true that you cannot add elements to both data structures. Python tuple does not support assignment, that is you cannot reassign individual elements in a tuple, while you can in java Array.
Java's ArrayList is similar to Python's List.
Nicer than Array for adding and removing items.
Java's Array has fixed length like you
mentioned.
Not sure what its equivalent in Python would be.
Related
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).
I have a string array where I want to check each element in the array with 8 other array elements to see if any of those element in the first array element categorize under any of them.
Simply I want to categorize a string array. So in order to do that I have to check with 8 other arrays (Because I have 8 categories) I want to know an efficient way to do this without looping one by one
You can use HashMap instead of array or arraylist.
Implement the 8 categories as HashMap.
From the element you want to check, match it against the the 8 HashMaps. This will give you a worst case of 8 checks.
If you can combine the values of all 8 categories into one HashMap, the worst case will be 1.
I believe you could combine the values from various categories and have only 1 HashMap by appending a checksum to each value. Something like:
//values in the hash map
xxxx_cat1
yyyy_cat1
zzzz_cat1
xxxx_cat2
yyyy_cat2
zzzz_cat2
After you get the value from the HashMap, just based on the checksum (the appended text) to get its category.
You can sort the array and then use Arrays.binarySearch() method instead of looping one by one. It is more efficient way to search a particular element.
I am learning about array as a data structure and I'm interested in how exactly in Java we obtain an element in an array by index.
What's happening under the hood when the following code is executed:
...
int i = array[2];
How JVM stores refferences to primitive types in array? How we obtain the element in O(1)? Does JVM calculates position of the element relative other element?
Arrays like int[], double[], etc. Is native arrays.
The Reference to native array is the reference to its first element.
Element with index "i" will be getted by dereferencing ("reference on the first element" + i * sizeOf("contained type")).
P.s. In the Java they are wrapped in other class for checking bounds, getting length and so on. In the low-level programming languages like C++ you can broke your programm, when u will try to change element by index grater than array length
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.
how can i store multiple strings in single array element in java? The strings may be added or deleted dynamically. So, the array has to be flexible. I mean, if there is an array a[], then:
a[1] should be able to accommodate "String1", "String2", ....
a[2] should be able to accommodate "String3", "String4", ....
These strings may be deleted at any time and additional strings can be added any time. What java component supports this kind of functionality? I am new to java and weak at basics. Hope u can help. Thanks !
You probably need something like List<List<String>> yourList ;
Remove "String2" from your List:
yourList.get(0).remove("String2");
Add "StringX" to your List at index 0.
yourList.get(0).add("Stringx");
My suggestion is to try and learn Java collections!
Official Tutorials
Tutorial 1 Tutorial 2
This way you will have a collection for any situation you find yourself in
You can add this strings in the same string using some using a character as delimiter and after that use a split method.
Use Jagged arrays. I can get a program to you to know the basics of jagged arrays: Jagged Arrays – Varying Column Size Arrays