I have an assignment, in which we have to create a directory which stores names and an extension number. In the first part we have to store the information in just an Array. We have to be able to add an entry to the Array and also initially read a .txt file and store it in the Array. Finally it has to print the Array. I was just wondering what would be more efficient because I only started learning code the last few months and this is difficult for me.
1) Would it be better to first create an Array of size [9999] (because that is the max number of entries the directory could have because everyone has a unique four digit number). I have an INT called count which keeps a count of what the last Array[x] to be added so when I call a method like addEntry, it will add the Entry to [X+1].
The problems with this be is that from what I have read, Arrays are huge and it could have performance problems. Also I have to be able to make a 'lookup' method which should be able to search the Array for someones number by entering their name.
My other option:
2) When the file is read, it counts how many lines the file has (each entry will be on its own line) then creates an Array of the number of lines in the text file. Whenever I want to add a new Entry, I copy the Array into an ArrayList, then back to an Array with the new Entry added. However I also read that copying arrays is not so good too and in a realistic view, if someone was to use this directory they could be adding entries every day.
Aslong as I store in an Array and print the Array into a table it is all good, but what would be the best way to make the actual Array when you don't know the size? (Yes I know I can use ArrayList but its specifically said not to)
Also how would you go about deleting an Entry in the Array, if I use my way there would be a missing entry at Array[x] where x got deleted.
Thanks in advance for any replies.
It is best to initialize an array to the size that you initially need it. When you need to add items to the array after that, you can uses a memory copy rather than an object clone.
int[] arrayOne = new int[20];
int[] arrayTwo = new int[40];
System.arraycopy(arrayOne, 0, arrayTwo, 0, arrayOne.length);
This copy is actually very fast because it just makes a direct copy from memory. This is actually what the underlying code of array list does when it doubles in size.
If you want a "dynamic" sized array, you could write your own version of array list.
For example:
Starts at size 1
Doubles in size when it gets full
shifts objects on a delete using System.arraycopy()
reduces in half when a delete causes size to be less than n/3
Related
It's been a while since I took a proper course on Java and I'm hoping someone can confirm/correct my understanding.
Consider the variables int[] arr and ArrayList arrLi:
arr has pointers directly to each component. arr[3] goes directly to the fourth element whereas arrLi.get(3) would have to traverse through the first three elements to get to the fourth.
Reassigning a component, such as a[3] = 0, does not rewrite the entire array.
Each time you want to add an element to arr, you would need to rewrite the entire array. For example, if there are 100 elements in arr, you have to make a new array with size 101 and copy all the elements from arr then add the new one. If you later decide to add yet another element, you'd have to go through the whole process again to add the 102-nd element.
arrLi adds (to end, front, or middle) and removes elements very efficiently because all it does is add/remove nodes and adjust the links.
ArrayList is a resizable array implementation of the List interface. Therefore fetching an element does not require traversing the previous elements.
Rewriting a value does not require rewriting the entire array in either case.
Yes, an array does need to be recreated if you need more space.
While it is called a list, ArrayList internally behaves much more like an array. ArrayList sometimes needs to be resized, meaning the underlying array needs to be recreated. However, this happens infrequently enough to not affect the average performance of an ArrayList over an array by much.
Please refer to https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html for more information
How can I add an element to an array while expanding the length of the array by one unit without using ArrayList? I'm a student, and it's forbidden for the assignment.
I have written a method that does it (sort of). It increments the array length every time its called but it only saves the last input parameter tot the very last element (every other element is made null when I create the new array with new length). The method is called in my constructor that makes an instance of an object from every line scanned in a text file, and I am supposed to add this line to an instance array.
You can't.
You can only create a new array which is longer than the old one. Other references of your program, already having a reference to the old array, are not affected and keep the old sized array.
I want to know how to add or append a new element to the end of an array. Is any simple way to add the element at the end? I know how to use a StringBuffer but I don't know how to use it to add an element in an array. I prefer it without an ArrayList or list. I wonder if the StringBuffer will work on integers.
You can not add an element to an array, since arrays, in Java, are fixed-length. However, you could build a new array from the existing one using Arrays.copyOf(array, size) :
public static void main(String[] args) {
int[] array = new int[] {1, 2, 3};
System.out.println(Arrays.toString(array));
array = Arrays.copyOf(array, array.length + 1); //create new array from old array and allocate one more element
array[array.length - 1] = 4;
System.out.println(Arrays.toString(array));
}
I would still recommend to drop working with an array and use a List.
Arrays in Java have a fixed length that cannot be changed. So Java provides classes that allow you to maintain lists of variable length.
Generally, there is the List<T> interface, which represents a list of instances of the class T. The easiest and most widely used implementation is the ArrayList. Here is an example:
List<String> words = new ArrayList<String>();
words.add("Hello");
words.add("World");
words.add("!");
List.add() simply appends an element to the list and you can get the size of a list using List.size().
To clarify the terminology right: arrays are fixed length structures (and the length of an existing cannot be altered) the expression add at the end is meaningless (by itself).
What you can do is create a new array one element larger and fill in the new element in the last slot:
public static int[] append(int[] array, int value) {
int[] result = Arrays.copyOf(array, array.length + 1);
result[result.length - 1] = value;
return result;
}
This quickly gets inefficient, as each time append is called a new array is created and the old array contents is copied over.
One way to drastically reduce the overhead is to create a larger array and keep track of up to which index it is actually filled. Adding an element becomes as simple a filling the next index and incrementing the index. If the array fills up completely, a new array is created with more free space.
And guess what ArrayList does: exactly that. So when a dynamically sized array is needed, ArrayList is a good choice. Don't reinvent the wheel.
The OP says, for unknown reasons, "I prefer it without an arraylist or list."
If the type you are referring to is a primitive (you mention integers, but you don't say if you mean int or Integer), then you can use one of the NIO Buffer classes like java.nio.IntBuffer. These act a lot like StringBuffer does - they act as buffers for a list of the primitive type (buffers exist for all the primitives but not for Objects), and you can wrap a buffer around an array and/or extract an array from a buffer.
Note that the javadocs say, "The capacity of a buffer is never negative and never changes." It's still just a wrapper around an array, but one that's nicer to work with. The only way to effectively expand a buffer is to allocate() a larger one and use put() to dump the old buffer into the new one.
If it's not a primitive, you should probably just use List, or come up with a compelling reason why you can't or won't, and maybe somebody will help you work around it.
As many others pointed out if you are trying to add a new element at the end of list then something like, array[array.length-1]=x; should do. But this will replace the existing element.
For something like continuous addition to the array. You can keep track of the index and go on adding elements till you reach end and have the function that does the addition return you the next index, which in turn will tell you how many more elements can fit in the array.
Of course in both the cases the size of array will be predefined. Vector can be your other option since you do not want arraylist, which will allow you all the same features and functions and additionally will take care of incrementing the size.
Coming to the part where you want StringBuffer to array. I believe what you are looking for is the getChars(int srcBegin, int srcEnd,char[] dst,int dstBegin) method. Look into it that might solve your doubts. Again I would like to point out that after managing to get an array out of it, you can still only replace the last existing element(character in this case).
one-liner with streams
Stream.concat(Arrays.stream( array ), Stream.of( newElement )).toArray();
I'm trying to make a programme that it doesn't ask user the value i, where i being the number of elements of an array, ( int[] someArray = new int[i]).
Two problems i'm facing , first how the programme auto locates memory size , and second one is, facing trouble with different types of data( i know this one is trivial but just could't put together the logic).
Basically my programme structure is like below:
Scanner input = new Scanner(System.in);
int[] someArray;
int element;
String order;
while(!("done").equals(order=input.nextLine())){
if(some integer){
//set the user input as the value of array element, and change the pointer to the next element
}
if(some string other than "done"){
System.out.println();
//continues the loop
}
}
You can just use an already existing growing collection, such as ArrayList, LinkedList, etc. You add as many elements as you want and they take care of dynamically allocating the necessary space.
This is called dynamic resizing and has the amortized complexity O(n).
The main idea is to double the array size every time your array gets full.
For implementation details I would take a look here.
PS: don't forget to mark some answers for your questions as SOLVED, as it seems that you never do that.
I want to use an ArrayList (or some other collection) like how I would use a standard array.
Specifically, I want it to start with an intial size (say, SIZE), and be able to set elements explicitly right off the bat,
e.g.
array[4] = "stuff";
could be written
array.set(4, "stuff");
However, the following code throws an IndexOutOfBoundsException:
ArrayList<Object> array = new ArrayList<Object>(SIZE);
array.set(4, "stuff"); //wah wahhh
I know there are a couple of ways to do this, but I was wondering if there was one that people like, or perhaps a better collection to use. Currently, I'm using code like the following:
ArrayList<Object> array = new ArrayList<Object>(SIZE);
for(int i = 0; i < SIZE; i++) {
array.add(null);
}
array.set(4, "stuff"); //hooray...
The only reason I even ask is because I am doing this in a loop that could potentially run a bunch of times (tens of thousands). Given that the ArrayList resizing behavior is "not specified," I'd rather it not waste any time resizing itself, or memory on extra, unused spots in the Array that backs it. This may be a moot point, though, since I will be filling the array (almost always every cell in the array) entirely with calls to array.set(), and will never exceed the capacity?
I'd rather just use a normal array, but my specs are requiring me to use a Collection.
The initial capacity means how big the array is. It does not mean there are elements there. So size != capacity.
In fact, you can use an array, and then use Arrays.asList(array) to get a collection.
I recomend a HashMap
HashMap hash = new HasMap();
hash.put(4,"Hi");
Considering that your main point is memory. Then you could manually do what the Java arraylist do, but it doesn't allow you to resize as much you want. So you can do the following:
1) Create a vector.
2) If the vector is full, create a vector with the old vector size + as much you want.
3) Copy all items from the old vector to your new vector.
This way, you will not waste memory.
Or you can implement a List (not vector) struct. I think Java already has one.
Yes, hashmap would be a great ideia.
Other way, you could just start the array with a big capacity for you purpose.