How can I solve java.lang.ArrayStoreException - java

Getting the following error in Android Studio:
Getting:
java.lang.ArrayStoreException: source[0] of type com.example.glide.ImageList cannot be stored in destination array of type java.lang.String[] in this line
Code:
ArrayList<ImageList> list = new ArrayList<>();
Object[] objNames = list.toArray();
String[] strNames = Arrays.copyOf(objNames, objNames.length, String[].class);
for(int j=0; j < strNames.length; j++){
strNames[j] = String.valueOf(list.get(j));
Log.d("String : {} ", strNames[i]);
}

Your program has lots of logical mistakes.
But try this one
public static void main(String[] args) {
ArrayList<ImageList> list = new ArrayList<>();
list.add(new ImageList()); //adding some dummy objects, you add yours
list.add(new ImageList());
String[] strNames = new String[list.size()];
for(int j=0; j < strNames.length; j++){
strNames[j] = String.valueOf(list.get(j));
Log.d("String : {} ", strNames[i]);
}
}

First, you have a list of ImageList items.
Second, this list is empty.
Third, you have defined an Array of String , and use Array.copyOf to populate it.
This won't work. You can cast ImageList to String directly.
What is that you are trying to achieve here?
Not sure, if you want a string representation of ImageList you can iterate over your "list" and call the .toString() method on each instance so you get the string representation of your instance.
Again not clear what you want?

Related

forming new small lists from a combination set of two lists

I have two patterns of lists inside a big list.
[[5.35, 5.09, 4.95, 4.81, 4.75, 5.19], [3601.0, 3602.0, 3603.0, 3600.0, 3610.0, 3600.0],[..,..,..,],[..,..,..],...]
To put in simple words, it is a combination of
[ [pricesList1], [DurationList1], [PricesList2], [DurationList2],... ]
I now want to create a new list with the price and corresponding duration from both lists as a pair from each set. For Example :
[[[5.35,3601.0],[5.09,3602.0],[4.95,3603],[4.81,3600],[4.75,3610],....],[[p1,d1],[p2,d2],[p3,d3],..],[[],[],[],..],....]
I have tried using List<List<Object>> and List<List<String>>. But no use. How can I do this?
I programed as following, which is wrong :
List<List<Object>> DurationList = new ArrayList<List<Object>>();
List<List<Object>> FinalList = new ArrayList<List<Object>>();
List<List<String>> SlotList = null;
for(int pair=0; pair<(FinalList.size()-1) ; pair=pair+2)
{
for(int innerloop=0; innerloop<(FinalList.get(pair).size());innerloop++)
{
SlotList = new ArrayList<List<String>>();
SlotList.addAll((Collection<? extends List<String>>) (FinalList.get(pair).get(innerloop)));
}
}
for(int pair=1; pair<(FinalList.size()) ; pair=pair+2)
{
for(int innerloop=0; innerloop<(FinalList.get(pair).size());innerloop++)
{
SlotList.addAll((Collection<? extends List<Object>>) FinalList.get(pair).get(innerloop));
}
}
Assuming the input list always has an even number of sublists and pairs of sublists have the same size, you can use a for loop iterating over the outer lists's element two by two :
List<List<String>> result = new ArrayList<>();
for (int i=0; i<outerList.size(); i+=2) {
List<String> priceList = outerList.get(i);
List<String> durationsList = outerList.get(i+1);
for (int j=0; j<priceList.size(); j++) {
List<String> newEntry = new ArrayList<>();
newEntry.add(priceList.get(j));
newEntry.add(durationsList.get(j));
result.add(newEntry);
}
}
As commented I suggest defining your own class to store the price and duration rather than using that List<String> newEntry.

ArrayList<Objects[]> to strings to use in SQL IN statement

I am trying to build a string to pass it as an SQL query within the IN statement.
ArrayList<Object[]> arrayList = new ArrayList<Object[]>();
List<String> strings = new ArrayList<>(arrayList .size());
for (Object object : arrayList ) {
strings.add(Objects.toString(object, null));
}
System.out.println("askldnlkasdn"+strings);
This still prints out the memory locations instead of the actual string
askldnlkasdn[[Ljava.lang.Object;#7bb11784, [Ljava.lang.Object;#33a10788, [Ljava.lang.Object;#7006c658, [Ljava.lang.Object;#34033bd0, [Ljava.lang.Object;#47fd17e3, [Ljava.lang.Object;#7cdbc5d3, [Ljava.lang.Object;#3aa9e816, [Ljava.lang.Object;#17d99928, [Ljava.lang.Object;#3834d63f, [Ljava.lang.Object;#1ae369b7]
I have also tried out
using StringBuilder and StringUtils. But things dont seem to work.
Any inputs as to where the problem is?
you should override method toString in your objects
You can use an SQL specific java Array.
try (PreparedStatement stmt = connection.prepareStatement("... IN (?) ...")) {
Object[] elements = ...
stmt.setArray(1, connection.createArray("TEXT", elements));
stmt.executeUpdate();
}
The problem you have is that you are implicitly using the toString() method of the Object elements inside your ArrayList. By default, that method returns the class and address of the Object. You should override the toString() method in every class you will use inside the list so it returns what you want it to.
This is new code that may help,
// Data of Array of Object for test the Code
Object[] a = new Object[1];
a[0] = "Hello";
Object[] b = new Object[1];
b[0] = "Friend";
Object[] c = new Object[1];
c[0] = "This is";
Object[] d = new Object[1];
d[0] = "Just Test";
// The Array List of objects and the data entry
ArrayList<Object[]> arrayList = new ArrayList<Object[]>();
arrayList.add(a);
arrayList.add(b);
arrayList.add(c);
arrayList.add(d);
// New List of strings
List<String> strings = new ArrayList<>(arrayList .size());
// The Process of adding the data from array list of objects to the strings
for(int i = 0; i < arrayList.size(); i++){
strings.add((String) arrayList.get(i)[0]);
}
// Just for print the data to console
for(int i = 0 ; i < strings.size(); i++){
System.out.println(strings.get(i));
}
System.out.println("askldnlkasdn "+strings.get(0));
I hope that solve the problem, if not please inform me, you can use it for more than one dimensional array.
You can just save it as String , like this code
ArrayList<String> arrayList = new ArrayList<>();
List<String> strings = new ArrayList<>(arrayList .size());
for (Object object : arrayList ) {
strings.add(Objects.toString(object, null));
}
System.out.println("askldnlkasdn"+strings);
Or you want it Object for specific purpose?

Copying the first two values of an array into a new array

I have sorted an array of objects into descending order based on one of their variables. I now need to take the first two values of this array and place them in a new String array. What I have so far is below, but Im obviously making a stupid mistake somewhere.
public String[] teamsToProgress()
{
Arrays.sort(teams);
String[] teamsToProgress = new String[2];
for (int i=0; i<2 ; i++)
{
teams[i] = teamsToProgress[i];
}
}
You try to assign String to a Team field (here: teams[i] = teamsToProgress[i];). You'll have to convert (not casting) the String to a Team instance before assigning.
If your just looking for a logical error, here it is:
teams[i] = teamsToProgress[i]; should be replaced to teamsToProgress[i] = teams[i];
I'm not getting into the details of the syntax involved. But I guess this is what you wanted based on your question.
teams[i] = teamsToProgress[i]; should be reversed, this make no sense. You're also missing a return statement.
public String[] teamsToProgress()
{
Arrays.sort(teams);
String[] teamsToProgress = new String[2];
for (int i=0; i<2 ; i++)
{
teamsToProgress[i] = teams[i]; //.getSomething() ?
}
return teamsToProgress;
}
your question is not clear enough as we still need to learn about Team object structure. but I'm guessing you are looking after something like this.
public String[] teamsToProgress(Team[] teams) //pass your sorted teams array as a param.
{
String[] teamsToProgress = new String[2];
for (int i=0; i<2; i++)
{
teamsToProgress[i] = String.valueOf(teams[i]); //convert teams[i] to string
}
return teamsToProgress;// you need to return an array
}

Swings: storing the selected values from List into an array.

I made a List in java as under:
String names[] = {"abc#gmail.com", "def#gmail.com","ghi#gmail.com","jkl#gmail.com"};
JList places = new JList(names) ;
Then in order to access the selected values I wrote in valueChanged method :
String[] emailID= places.getSelectedValuesList().toString[];
which is coming out to be incorrect ... Kindly help how should I rewrite this line so as the selected values get stored in array.
If you want to have all selected Items as an Array you can do something like this:
public static void main(String[] args){
String names[] = {"abc#gmail.com", "def#gmail.com","ghi#gmail.com","jkl#gmail.com"};
JList<String> places = new JList<String>(names) ;
places.setSelectedIndices(new int[]{0,1,2});
String[] emailIDs = places.getSelectedValuesList().toArray(new String[]{});
for(String s : emailIDs){
System.out.println(s);
}
}
Note:
I added <String> to the List, because I assume you always want to have Strings as values. That way you can get the List .toArray() method with a generic output. Else you'd need to get an Object[] (Object Array) and cast the values.
For Storing Selected Items in String Array you can try this
Object[] values = places.getSelectedValues();
String[] strings = new String[values.length];
for(int i = 0; i < values.length; i++) {
if(values[i] instanceof String) {
strings[i] = ((String) values[i]);
}
}

Java List list to String []

How would I convert
List list= new ArrayList();
to
String [] profArr= {};
I have tried doing
profArr = list.toArrary()
and
profArr = (String [])list.toArrary()
I get the following error:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
I also have tried
String [] profArr= (String [])list.toArray(new String[0]);
and I get this error: The requested resource () is not available.
Here is how I create the list:
static List decode(int x)
{
List power2List = new ArrayList();
if (x < 0)
throw new IllegalArgumentException("Decode does not like negatives");
while (x > 0)
{
int p2 = Integer.highestOneBit(x);
x = x - p2;
power2List.add(p2);
}
return power2List;
}
List list= new ArrayList();
list= decode(rset.getInt("favprofs")); //rset being a result set which pulls one int
You need to be using list.toArray(new String[list.size()]). An Object[] is not type compatible with String[], despite every element in the Object[] being a String. Also, you should consider specifying the type parameter of your List to maintain type safety.
Basically you need to use
String profArr = list.toArray( < String array > (size))
you can use java collector
List<Tuple2<String, Boolean>> paras_check
String miss_params= paras_check.stream().map(e->e._1)
.reduce(",",(x,y)->(x+y));

Categories

Resources