In a mapreduce code, while converting list to array of (org.apache.hadoop.io.Text) Text type, receiving ArrayStoreException.
List<String> testList= new ArrayList<String>();
testList.add("testData1");
testList.add("testData2");
Text[] testArray=testList.toArray(new Text[testList.size()]);
but when i dont add any value to the list and then convert it to array, it works fine(with null values). Can some one please point my mistake.
You can't store Strings in a Text array (Text[]).
You can store them in a String array (String[]) :
String[] testArray=testList.toArray(new String[testList.size()]);
when i dont add any value to the list and then convert it to array, it works fine
It only works since you are creating an empty array in this case, so nothing is stored in it.
If you must produce a Text[] that contains the data from the source List<String>, you have to iterate over the List and produce the Text instances yourself:
Text[] testArray = new Text[testList.size()];
for (int i = 0; i < testList.size(); i++) {
testArray[i] = new Text(testList.get(i)); // assuming the Text class
// has such a constructor
}
Related
I have an ArrayList and this ArrayList have other array inside, I need to obtain the data inside this other array.
This is the structure of the ArrayList and the data inside, I need to get that data
This is the code that I use for get all the ArrayList but I need to get the other array that is store in the index(0) and when I can get this data I need to store it in an new Array
if (msg.equals("warning")) {
ArrayList<String> dataWarn = (ArrayList<String>) data;
Log.i("api", dataWarn.toString());
To access the inner ArrayList:
ArrayList<String> innerList = data.get(0);
Then you can extract the data inside:
String value = innerList.get(0);
And add it to some otherList:
List<String> otherList = Arrays.asList(value);
You could do the following:
String[] newArray = new String[outerArray.size()];
for(int i = 0; i<outerArray.size();i++){
newArray[i]=outerArray.get(0).get(0);
}
I am new to Java and Here is my code.
String[][] datas={{"a","b","c"},{"d","e","f"},{"g","h","i"}};
String[] onedata={"j","k","l"};
the thing I want to do here is that, I want to append the onedata into datas at last index value.
Please help let me know that how can I do this.
You can use an ArrayList because their sizes are mutable. For example:
String[][] datas={{"a","b","c"},{"d","e","f"},{"g","h","i"}};
List<String[]> datasList = new ArrayList<>(Arrays.asList(datas));
String[] onedata = {"j","k","l"};
datasList.add(onedata);
datas = datasList.toArray(new String[datasList.size()][]);
The things you are dealing with are arrays (String[]) and multidimensional arrays (String[][]) in Java, not lists. Their length is fixed. Therefore to append a new item to an array in such way that the length increases (so not by replacing the last item in the current array) you would need to create a new array with length n+1, assign the old values to the first n indices and then the new value to the index n+1.
I have declared an ArrayList of a generic string array
ArrayList<String[]> dataListCol = new ArrayList<String[]>();
I am populating the arraylist this way
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
String[] strArrayCol = new String[2];
strArrayCol[0] = json_data.getString("col1");
strArrayCol[1] = json_data.getString("col2");
dataListCol.add(strArrayCol);
}
I am attempting to print all the individual items in the array as shown
System.out.println("new array " + Arrays.toString(dataListCol.toArray()));
the output is not a meaningful string representation. Please how can I print all the items in the array
As each element itself is an array, you need to call Arrays.toString on each element to print the values, e.g.:
List<String[]> list = new ArrayList<String[]>();
list.stream()
.map(Arrays::toString)
.forEach(System.out::println);
Or, to print just one element, simply use the following inside the loop:
System.out.println(Arrays.toString(strArrayCol));
Update
If you want to assign the String to a reference instead of printing it then you can use Collector, e.g.:
String string = list.stream()
.map(Arrays::toString)
.collect(Collectors.joining("|"));
This will give you pipe separated string for all the elements.
Two way to do that, loop or foreach method.
for (String object: list) {
System.out.println(object);
}
or
ArrayList l=new ArrayList();
l.add(value);
l.forEach((a)->System.out.println(a));
hope that help you
The fact is that you are calling the method tostring() on the array that use the method for generic objects showing to you the hash of that specific object.
You can simply do something like this
int size = t.length;
for (int i=0; i<size; i++) { System.out.println(t[i]);
}
Or in a simpler way you can use
Arrays.toString()
And
System.out.println(Arrays.toString(yourArray));
What's Actually Happening
You've made a list that holds type String[], when you ask the list to return to you it's state represented by an array, the call to toArray(), it returns an array that contains each element in the list.
So the array you've received back is an Object[] that contains all the elements in the list. Remember what type of elements you declared the list to hold -
String[]
Arrays.toString(Object[] array) iterates through the array given calling toString() on each element. The runtime type of each element in the array you gave is String[], therefore on every iteration toString() is being called on an array.
What You Want to Happen
You want to call toString() on each String element in each array of String that the list is holding:
for(String[] strArray : dataListCol) {
System.out.println(Arrays.toString(strArray));
}
or if your a fan of spaghetti output:
System.out.println(Arrays.deepToString(dataListCol.toArray()));
Note Arrays.deepToString(dataListCol.toArray())) does what you wanted. This is because when focusing only on how elements are stored, essentially a List<Object[]> is equivalent to a 2D array, Object[][], just as a List<Object> is equivalent to a single dimension array Object[].
Except elements of an array are stored contiguously in memory whereas Collection classes do not store their elements contiguously (just to keep the memory heroes off my back)
I am looking for a solution how I can get an array of contains from another one.
For example:
I have an array:
Array= [S1!!T1, S1!!T2, S1!!T3, S2!!T1, S2!!T2, S3!!T1, S3!!T2, S3!!T3]
I am looking for elements in "Array" that contain "S2" and write them to another one. so i should get:
Result = [S2!!T1, S2!!T2]
I already tried the Arrays.asList(I).contains(i) but this is not what i am lookig for i think.
If you wish to copy elements of one array to another, first thing you need to do is loop through the elements of one array and if you find a match then store it into another array.
Let's say you have the following array:
String[] arr = new String[]{"S1!!T1", "S1!!T2", "S1!!T3", "S2!!T1", "S2!!T2", "S3!!T1", "S3!!T2", "S3!!T3"};
We don't know how many of those elements in the array are going to match until we loop through them so we have two choice:
Create another array with the same size as arr (cause some null values in array if not all entries of arr re matched)
Use ArrayList and then later convert ArrayList to array if needed
See below:
public static void main(String[] args) {
String[] arr = new String[]{"S1!!T1", "S1!!T2", "S1!!T3", "S2!!T1", "S2!!T2", "S3!!T1", "S3!!T2", "S3!!T3"};
List<String> s2List = new ArrayList<String>();
//loop through arr and for each element check if it contains S2
for(int i = 0; i < arr.length; i++) {
//if it contains S2 then it returns true and we add it to list
if(arr[i].contains("S2")) {
//add to list the element
s2List.add(arr[i]);
}
}
//print the list for testing
System.out.println(s2List);
//if you wish to store the elements to array then
//now we know how many matched, so we can create array with the
//size of elements in s2List
String[] sArr = new String[s2List.size()];
//Here loop through the list and assign values to array
for(int i = 0; i < s2List.size() ; i++) {
sArr[i] = s2List.get(i);
}
//print the array
System.out.println(Arrays.toString(sArr));
}
You can also use other methods that convert a List to array directly but, above should give you an idea of how to resolve the question you asked.
You could use Java 8 streams
String[] filtered = Stream.of(strings).filter(s -> s.contains("S2")).toArray(String[]::new);
hello I have a commaseparated list of string and put into an array.
I ultimately need them as a list of shorts, but the only way I know how to do that is get them as an array of shorts then do array.asList()
String[] stringArray= commaSeparatedString.split("\\s*,\\s*");
How can I get that to an array of shorts so I can throw in a list?
THanks
Well presumably you just need to parse each element. But why not just add them to an ArrayList<Short>?
List<Short> shortList = new ArrayList<Short>(stringArray.length);
for (int i = 0; i < stringArray.length; i++) {
shortList.add(Short.valueOf(stringArray[i]));
}
(Note that you can't have a list of a primitive type, as Java generics don't support primitive type arguments.)
After the splitting you parse each string to the desired type for example Short.parseShort(element)