i have 2d array in this form:
GrupaArtikala [] grupaArtikala = parser.getObjektiArtikli();
Object[][] data = {
{grupaArtikala[1].getId(), grupaArtikala[1].getSifra(), grupaArtikala[1].getNaziv(), grupaArtikala[1].getIkonaID()},
{grupaArtikala[2].getId(), grupaArtikala[2].getSifra(), grupaArtikala[2].getNaziv(), grupaArtikala[2].getIkonaID()},
{grupaArtikala[3].getId(), grupaArtikala[3].getSifra(), grupaArtikala[3].getNaziv(), grupaArtikala[3].getIkonaID()},
{grupaArtikala[4].getId(), grupaArtikala[4].getSifra(), grupaArtikala[4].getNaziv(), grupaArtikala[4].getIkonaID()},
{grupaArtikala[5].getId(), grupaArtikala[5].getSifra(), grupaArtikala[5].getNaziv(), grupaArtikala[5].getIkonaID(),}
};
But i wat to create it with nested for loop, Any help please?
Here is one way you could incorporate a loop into this:
GrupaArtikala[]grupaArtikala=parser.getObjektiArtikli();
int length = grupaArtikala.length;
Object[][] data = new Object[length][4];
for(int i=0;i<length;i++){
GrupaArtikala temp = grupaAtrikala[i];
data[i][0] = temp.getId();
data[i][1] = temp.getSifra();
//add the rest of your attributes
}
Related
Hi.
I'm making an app that receives data from bluetooth by using stringbuilder
And makes it slice for using another activity.
The image shows what i want to make.
Q1. What should i use c->d, d->e ?
Q2. There will be a lot of data, I want to know the way to simplify this sequence
******************** edited ********************
I have practiced by adding value to Arraylist.
But in String Array, there is no .get(), so i couldn't access to element's length.
public static ArrayList<String> randomValue = new ArrayList<>();
public static int iDistance=0, xIAngle=0, yIAngle=0, zIAngle=0;
public static String distance, xAngle, yAngle, zAngle;
randomValue.add("12345090080070");
randomValue.add("15640080085071");
randomValue.add("16542070084074");
randomValue.add("12645080087078");
randomValue.add("21345084081060");
randomValue.add("14785078075065");
randomValue.add("13155079077077");
randomValue.add("14623080078078");
randomValue.add("14918086080078");
randomValue.add("15684085082080");
for (int i=0; i<randomValue.size(); i++){
String a = randomValue.get(i);
String distance = a.substring(0,5);
String xAngle = a.substring(5,8);
String yAngle = a.substring(8,11);
String zAngle = a.substring(11,14);
//String to int
iDistance = Integer.parseInt(distance);
xIAngle = Integer.parseInt(xAngle);
yIAngle = Integer.parseInt(yAngle);
zIAngle = Integer.parseInt(zAngle);
}
It seems like you are just stuck on finding the equivalent of get for a string array. To access an element in an array, the syntax is array[I], so if you were using a string array, this line:
String a = randomValue.get(i);
would have been:
String a = randomValue[i];
The code for your sequence of transformations can be shortened with Streams:
// this is the sequence of transformation starting with the sting builder "a"
List<String> randomValueWithLength14 =
Arrays.stream(a.toString().split(";")).filter(x -> x.length() == 14)
.collect(Collectors.toList());
// this is the for loop shown in your code
for (int i=0; i<randomValueWithLength14.size(); i++){
String s = randomValueWithLength14.get(i);
String distance = a.substring(0,5);
String xAngle = s.substring(5,8);
String yAngle = s.substring(8,11);
String zAngle = s.substring(11,14);
//String to int
iDistance = Integer.parseInt(distance);
xIAngle = Integer.parseInt(xAngle);
yIAngle = Integer.parseInt(yAngle);
zIAngle = Integer.parseInt(zAngle);
}
I have a dataset with so many columns and I want to cast all columns to the string using Java.
I tried below steps, I want to know if there is any better way to achieve this?
Dataset<Row> ds = ...;
JavaRDD<String[]> stringArrRDD = ds.javaRDD().map(row->{
int length = row.length();
String[] columns = new String[length];
for(int i=0; i<length;i++){
columns[i] = row.get(i) !=null? row.get(i).toString():"";
}
return columns;});
You can iterate over columns:
for (String c: ds.columns()) {
ds = ds.withColumn(c, ds.col(c).cast("string"));
}
If you want to use objects only:
import org.apache.spark.sql.types.*;
...
for (String c: ds.columns()) {
ds = ds.withColumn(c, ds.col(c).cast(DataTypes.StringType));
}
I get the data type of data from sqlite database is string. How to put these data into ArrayList()? Thank you!
try below code:-
ArrayList<String> array = new ArrayList<String>();
Levels l = new Levels(getBaseContext()); // level ur table name
l.open();
Cursor c_l = l.selectAll();
for (int i = 0; i < c_l.getCount()+1; i++)
{
array.add(c_l.getString(0)); // getstring 0 means your column
}
c_l.close();
l.close();
I cannot retrieve the selection in the JComboBox that was selected by the user. the J ComboBox inclued a list of car registration numbers, which then the user has to select one and it will be added to the booking ArrayList. Unfortunately it is not working properly. and the booking is not being saved because of this. Please Help Me! Maybe I need to change the get Method for the combo box.
ArrayList BookingList = CarRentalSystem.BookingList;
ArrayList CarList = CarRentalSystem.CarList;
UsingFiles BookingFile = CarRentalSystem.BookingFile;
String [] regNums;
public NewBooking() {
regNums = new String[CarList.size()+1];
for (int i = 0; i< CarList.size();i++){
regNums[i] = ""+((Car)CarList.get(i)).getCarNum();
}
initComponents();
....
Booking book = new Booking();
String regNum = cmbCar.getActionCommand();
book.getRegNum();
Not easy to understand what u want can you add more code?
try this to convert your list into a String Array that u pass in as a argument to your combobox.
public String[] regNums() {
String tArray[] = null;
for (int i=0; i<carList.size(); i++){
//Convert into a String[] and put the arrayList values in it.
tArray = carList.toArray(new String[i]);
}
}
I had create an Array[] and i would like to save it in firsti column! So i had wrote this code:
String[][] SundaySounds = {{"0","0"}};
String[] SundayArray = {"0"};
........
for (int i=0;i<SundayArray.length;i++)
{
SundaySounds[0][i] = SundayArray[i]; <--line 39
System.out.println(SundayArray[0][i]);
}
And i get this error output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Test.main(Test.java:39)
Arrays in Java start at position 0, not position 1.
String[][] SundaySounds = {{"0","0"}};
String[] SundayArray = {"0"};
for (int i=0;i<SundayArray.length;i++)
{
SundaySounds[0][i] = SundayArray[i];//changed the index
System.out.println(SundayArray[i]);
}
This doesnt work, because in the second iterations you do the following:
SundaySounds[0][i] = SundayArray[i];
Here is i equal to 1. This is not possible becausw SundayArray doenst go further than 0
The following code works just fine.....
String[][] SundaySounds = {{"0","0"}};
String[] SundayArray = {"0"};
for (int i=0;i<SundayArray.length;i++)
{
SundaySounds[0][i] = SundayArray[i];
}
System.out.println(SundaySounds[0][0]);
System.out.println(SundaySounds[0][1]);
Output comes as 0 0
Try working it again.