array of structure of strings - java

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

Related

Java efficiency (Calling method)

I'm writing a method that finds intersection of given arrays.
Since I had to iterate both arrays, I was thinking of using
if(arr2.length > arr1.length){
intersection(arr2, arr1);
}
The reason I came up with this idea was that it seemed to me to be the better way to reduce the length of code for handling arrays that have different length.
As a newbie to programming, I'm wondering if there is any other suggestion.
Put your array in a list:
List a = List.asArray(arr1);
(ref: https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...)
And use the suggestion here to find the intersection:
How to do union, intersect, difference and reverse data in java

how do i print an element from a 2 dimension array-list?

The last command should print "printcat" but it does not. Why is that?
ArrayList<String[]> outerArr = new ArrayList<String[]>();
String[] myString1= {"hey","hey","hey","hey"};
outerArr .add(myString1);
String[] myString2= {"you","printcat","you","you"};
outerArr .add(myString2);
System.out.println(Arrays.toString(outerArr.get(1)));
System.out.println(outerArr.get(1).get(1));
I don't think it is a two dimensional Array-List. It's Just an Array-List which contains arrays of Strings as data.
You can use System.out.println(outerArr.get(1)[1]); to get the result.
outerArr.get(1) will return String[], which is {"you","printcat","you","you"}, and then you can use outerArr.get(1)[1], which will return the "printcat" element at the 1st index.
List, ArrayList and array ([]) are all different things (though the first two are related, and they all are kind of similar things). if you want something in 2D, it means you use something 2 times. for example a 2D List would look like List<List<String>> and a 2D array would look like String[][]. what you have is a list of arrays, which is not a 2D array, nor a 2D list. what you most likely want is List<List<String>> outerArr = new ArrayList<List<String>>();. The rest you should figure out yourself.
I finally figured it out! Because the Arraylist exists of Array you must handle the Array as a normal Array. So this works fine.
System.out.println(outerArr.get(1)[1]);

Data structure for holding the content of a parsed CSV file

I'm trying to figure out what the best approach would be to parse a csv file in Java. Now each line will have an X amount of information. For example, the first line can have up to 5 string words (with commas separating them) while the next few lines can have maybe 3 or 6 or what ever.
My problem isn't reading the strings from the file. Just to be clear. My problem is what data structure would be best to hold each line and also each word in that line?
At first I thought about using a 2D array, but the problem with that is that array sizes must be static (the 2nd index size would hold how many words there are in each line, which can be different from line to line).
Here's the first few lines of the CSV file:
0,MONEY
1,SELLING
2,DESIGNING
3,MAKING
DIRECTOR,3DENT95VGY,EBAD,SAGHAR,MALE,05/31/2011,null,0,10000,07/24/2011
3KEET95TGY,05/31/2011,04/17/2012,120050
3LERT9RVGY,04/17/2012,03/05/2013,132500
3MEFT95VGY,03/05/2013,null,145205
DIRECTOR,XKQ84P6CDW,AGHA,ZAIN,FEMALE,06/06/2011,null,1,1000,01/25/2012
XK4P6CDW,06/06/2011,09/28/2012,105000
XKQ8P6CW,09/28/2012,null,130900
DIRECTOR,YGUSBQK377,AYOUB,GRAMPS,FEMALE,10/02/2001,12/17/2007,2,12000,01/15/2002
You could use a Map<Integer, List<String>>. The keys being the line numbers in the csv file, and the List being the words in each line.
An additional point: you will probably end up using List#get(int) method quite often. Do not use a linked list if this is the case. This is because get(int) for linked list is O(n). I think an ArrayList is your best option here.
Edit (based on AlexWien's observation):
In this particular case, since the keys are line numbers, thus yielding a contiguous set of integers, an even better data structure could be ArrayList<ArrayList<String>>. This will lead to faster key retrievals.
Use Array List. They are arrays with dynamic size.
The best way is to use a CSV parser, like http://opencsv.sourceforge.net/. This parser uses List of String[] to hold data.
Use a List<String>(), which can expand dynamically in size.
If you want to have 2 dimensions, use a List<List<String>>().
Here's an example:
List<List<String>> data = new ArrayList<List<String>>();
List<String> temp = Arrays.asList(someString.split(","));
data.add(temp);
put this in some kind of loop and get your data like that.

Arraylist vs Array with custom objects in java

I am trying to convert a section of code from using an ArrayList of custom objects to a regular array.
Previous my definition was
ArrayList<Room> rooms = new ArrayList<Room>();
Which I have now changed to
Room[] rooms;
Previously I used the below line to add items to the array list
rooms.add(new Room(1,1,30,false,true,true,false));
But I am now struggling to find the way I should simply add individual items to the array throughout code.
I think you are best sticking with an arrayList here, but just to give you a bit more light on it.
To do what you are trying to do, you will have to keep a index integer which will just point to the current position in the array, then when you add you can increment this and add the new object into the next poisition.
When you get to the maximum size of your array, you will need to expand it.
You will find that there has been questions on expanding an array which have been asked already and you can find the answers here:
Expanding an Array?
If you can live with a fixed-size array, that gives you at least a slight chance of success. If not, you can't beat ArrayList and if your mission is to succeed without reimplementing it, then it is an impossible mission.
You should really give more insight into the exact rationale for rewriting your code like that, it would give us some chance to properly help you.
I can recommend:
Use Arraylist as Long you Need to insert Elements. Once th Array is final Convert to Array.
If you need to increase the size of the array when adding another element, you have to construct another array with the size of the old array +1.
Afterwards, you would copy the contents of the old array over to the new array (the bigger array).

Concatenating 2 2D arrays in java?

I've got 2 2D arrays, one int and one String, and I want them to appear one next to the other since they have the same number of rows. Is there a way to do this? I've thought about concatenating but that requires that they be the same type of array, so in that case, is there a way I could make my int array a String array?
If you want an array that can hold both Strings and ints, you basically have two choices:
Treat them both as Objects, so
effectively Object[][]
concatArray. Autoboxing will
convert your ints to Integers.
Treat them both as Strings (using
String.valueOf(int) and
Integer.parseInt(String)).
I don't know for a fact, but would guess autoboxing is a less expensive operation that converting ints to string and back.
Further, you can always find out the value type of a cell in the array by using instanceof operator; if values are converted to String, you actually need to parse a value to find out if its just a bit of text or a text representation of a number.
These two considerations -- one a guess, the other possibly irrelevant in your case -- would support using option 1 above.
Just cast the ints to Strings as you concatenate. The end result would be a 2D array of type String.
To concatenate an int to a String, you can just use
int myInt = 5;
String newString = myInt + "";
It's dirty, but it's commonly practiced, thus recognizable, and it works.
There are two ways to do this that I can see:
You can create a custom data object that holds the strings and ints. This would be the best way if the two items belong together. It also makes comparing rows easier.
You can create a 4D array of objects and put all the values together like this. I wouldn't recommend it, but it does solve the problem,
I hear the curse of dimensionality lurking in the background, that said - first answer that comes to mind is:
final List<int,String> list = new List<int,String>();
then reviewing op's statement, we see two [][] which raises the question of ordering. Looking at two good replies already we can do Integer.toString( int ); to get concatenation which fulfills op's problem definition, then it's whether ordering is significant or flat list and ( again ) what to do with the second array? Is it a tuple? If so, how to "hold" the data in the row ... we could then do List<Pair<Integer,String>> or List<Pair<String,String>>, which seems the canonical solution to me.

Categories

Resources