My app gets strings of images url from server and saves them in an array .the problem is number of images url is not constant so i want to create empty array of strings and its size determined according to input entered to it .
i used this Strings images=new Strings[5] and i want thing like that Strings images=new Strings[] so how i can do that this is part of my code
String[] images4=new String[3];
String[] images5=new String[3];
String[] images7=new String[3];
String[] images6=new String[3];
String[] images1=new String[3];
String[] images2=new String[3];
String[] images3=new String[3];
String[] totals=new String[3];
try {
JSONObject json = new JSONObject(Content);
JSONArray jre = json.getJSONArray("projects");
JSONArray jreimages = json.getJSONArray("screenshots");
mArrayList = new ArrayList<Projects>();
for (int j = 0; j < jre.length(); j++) {
mProduct = new Projects();
JSONObject jobject = jre.getJSONObject(j);
String name11 = jobject.getString("name");
String description = jobject.getString("description");
String status = jobject.getString("status");
String version = jobject.getString("version");
String id =jobject.getString("id");
mProduct.setyourText(name11);
mProduct.setyourstatu(status);
mProduct.setYourversion(version);
mProduct.setyourdescription(description);
int k=0;
int l=0;
int o=0;
int p=0;
int w=0;
for (int i = 0; i < jreimages.length(); i++) {
JSONObject jjobject = jreimages.getJSONObject(i);
String imageid=jjobject.getString("project_id");
if (imageid.equals(id)) {
String urlimage = jjobject.getString("screenshot");
String total = url + urlimage;
// mess.setText(total);
if (imageid.equals("105")){
images1[k] = total;
k++;
totals=images1;
}
else if (imageid.equals("106")){
images2[k] = total;
k++;
totals=images2;
}
else if (imageid.equals("107")){
images3[k] = total;
k++;
totals=images3;
}
else if (imageid.equals("108")){
images4[k] = total;
k++;
totals=images4;
}
else if (imageid.equals("109")){
images5[k] = total;
k++;
totals=images5;
}
else if (imageid.equals("110")){
images5[k] = total;
k++;
totals=images5;
}
else if (imageid.equals("111")){
images6[k] = total;
k++;
totals=images6;
}
else{
totals=images7;
}
}
}
mProduct.setYourimages(totals);
mArrayList.add(mProduct);
mProduct = null;
}
You can use two approaches:
String[] arr;
ArrayList<String> arrList= new ArrayList<>();
Don't create individual string array.You can use Arraylist to make a dynamic String array.
ArrayList<String> images =new ArrayList<String>();
to insert values.Just use add() method.
For example,You want to store iamgeUrls then :
images.add(image1);
images.add(image2);
And to retrieve data use get() method.Example :
images.get(position); //where position is the index of imageUrl you want to retrieve.
You can use:
String[] array= new String[0];
Or
ArrayList<String> array=new ArrayList<String>();
Related
I am creating an ArrayList of String Arrays in Java.. The code for the same is follows..
ArrayList<String[]> al = new ArrayList<String[]>();
for(int i = 0; i < t; i++) { // t is input by user representing size of arraylist
int k = sc.nextInt();
String[] s = new String[k]; // k string values input by user
}
Iterating to print the values of ArrayList
Iterator it = al.iterator();
while(it.hasNext()){
for(int i = 0; i < (it.next(new String[])).length; i++) { // error for dimension missing
System.out.println((it.next(new String[])).length); // error for dimension missing
}
}
I get an error of "array dimension missing" in the indicated lines. Please suggest how to convert ArrayList Object as String Array.
Try this
Iterator<String[]> it = al.iterator();
while(it.hasNext()){
String temp [] = it.next();
for(int i = 0; i < temp.length; i++) {
System.out.println(temp.length);
}
}
your new String[]
requires size, because you're making new array i.e
String[] myStringArray = new String[5];
also, your iteration to print values make no sense, try :
for(String[] stringArray: al) {
System.out.println("Size: " + stringArray.length);
for(String s: stringArray) {
System.out.println(s);
}
}
Wil this work, how can i compare a string array elements to a predefined string and copy to a new array.
String element={"France","Germany","USA","France","Italy"}
String finalelement[]= null;
String compareelement = "France";
int l =elements.length;
int i1 = 0;
for(int i=0; i<l; i++)
{
//comparing the elements
if((compareelement.equals(element[i])))
{
//assigning the array element
finalelement[i1]=element[i];
i1++;
}
}
It's better to do it this way:
List<String> elements = new ArrayList<>();
elements.add("France");
elements.add("Germany");
elements.add("USA");
elements.add("France");
elements.add("Italy");
List<String> finalElement = new ArrayList<>();
String compareElement = "France";
for (String str : elements) {
if (compareElement.equals(str)) {
finalElement.add(str);
}
}
At least the first line is wrong:
String element={"France","Germany","USA","France","Italy"}
String[] elements = new String[]{"France","Germany","USA","France","Italy"}
I am using Java 1.4 for an old project.
I have a string array
String a[0]={"200,300"}
String a[1]={"700,500"}
String a[2]={"900,400"}
as so on,
for this I need to create 2 string array to get the first value in a string array and second value in different string array.
ex:
String a[0]="200"
String a[1]="700"
String a[2]="900"
String b[0]="300"
String b[1]="500"
String b[2]="400"
I am not getting how can I do this.
If the length of the string array is known,
String[] newString = new String [6];
int count = 0;
for(int i = 0; i<3 ; i++){
newString[i] = a[i];
count++;
}
for(int j=0; j<3; j++){
newString[count] = b[j];
count++;
}
EDIT:
Previously i got your question wrong. The following solution will help you. to divide a into 2 string arrays.
String a[] = new String [3];
a[0]={"200,300"};
a[1]={"700,500"};
a[2]={"900,400"};
String[] newStr1 = new String [a.length];
String[] newStr2 = new String [a.length];
for(int i = 0; i<3 ; i++){
String [] x= a[i].split(",");
newStr1[i] = x[0];
newStr2[i] = x[1];
}
Looking at your code, this provides you the correct solution,
int n = 3; //This can change as your array size is not known
String a[] = new String [n];
a[0]="200,300";
a[1]="700,500";
a[2]="900,400"; //And So on
String[] b1 = new String [n];
String[] b2 = new String [n];
for(int i = 0; i<n ; i++)
{
String [] numbers= a[i].split(",");
b1[i] = numbers[0];
b2[i] = numbers[1];
}
First you cant defined String array as string a[1]={700,500}. It is wrong way.
And second, Here is what you want :
a[0]=new String[]{"200","300"};
a[1]=new String[]{"700","500"};
a[2]=new String[]{"900","400"};
String x[] = new String[a.length];
String y[] = new String[a.length];
for(int i=0;i<a.length;i++){
x[i] = a[i][0];
y[i] = a[i][1];
}
Am doing a simple android application.In that I am deleting an element from array using the following code.
arr_fav = {"1","2","3"};
for(int i= 0;i<arr_fav.length;i++)
{
if(current_id == Integer.parseInt(arr_fav[i]))
{
arr_fav[1] = null;
} }
By doing this am getting the array like arr_fav = {"1",null,"3"}.But I want like arr_fav = {"1","3"}.How to delete an element.Am new to this android development.Please help me to solve this.
its better to use arraylist
arr_fav = {"1","2","3"};
List<String> numlist = new ArrayList<String>();
for(int i= 0;i<arr_fav.length;i++)
{
if(current_id == Integer.parseInt(arr_fav[i]))
{
// No operation here
}
else
{
numlist.add(arr_fav[i]);
}
}
arr_fav = numlist .toArray(new String[numlist .size()]);
You don't.
Arrays can not be resized.
You would need to create a new (smaller) array, and copy the elements you wished to preserve into it.
A better Idea would be to use a List implementation that was dynamic. An ArrayList<Integer> for example.
Arrays in Java are not dynamic, you can use an ArrayList instead.
You can copy the array elements that you want into a new array
j = 0;
for(int i= 0;i<arr_fav.length;i++)
{
if(current_id != Integer.parseInt(arr_fav[i]))
{
arr_new[j++] = arr_fav[i];
} }
Use an ArrayList instead of an array. It supports features like deleting any element, dynamic size, and many more.
ArrayList<String> arr_fav_list = new ArrayList<String>();
arr_fav_list.addAll(arr_fav);
arr_fav_list.remove(1);
This will do the job ...
List x = new ArrayList(Arrays.asList(arr_fav));
x.remove(String.valueOf(current_id));
arr_fav = x.toArray();
try this:
ArrayList<String> rm = new ArrayList<String>();
rm .addAll(arr_fav);
rm .remove(1);
Try something like this
int[] intAry = new int[5];
// populate array with 0 to 4
for (int i=0; i < intAry.length; i++) {
intAry[i] = i;
}
List<Integer> aList = Arrays.asList(intAry); // change the array to a list of integers
aList.remove(3); // remove the item 3 (4th index)
aList.toArray(intAry); // convert list back to array
System.out.println("size of array=" + intAry.size()); // output array size should be 4
for (int i=0; i < intAry.length; i++) {
System.out.print(intAry[i] + " "); // should output "0 1 2 4 "
}
set
array_fav[1]=array_fav[2];
array_fav[2]=null;
You can do it using the following method..
public static String[] removeElements(String[] input, String deleteMe) {
List result = new LinkedList();
for(String item : input)
if(!deleteMe.equals(item))
result.add(item);
return result.toArray(input);
}
OR you could use ArrayUtils.
array = ArrayUtils.removeElement(array, element)
For simple arrays like this you can't do this in this way
here is the full sample code for this
int current_id = 2;
String[] arr_fav = { "1", "2", "3" };
for (int i = 0; i < arr_fav.length; i++) {
if (current_id == Integer.parseInt(arr_fav[i])) {
String[] arr_fav_tem = new String[arr_fav.length - 1];
arr_fav[1] = null;
int counter = 0;
for (int j = 0; j < arr_fav.length; j++) {
if (arr_fav[j] != null) {
arr_fav_tem[counter] = arr_fav[j];
counter++;
}
}
arr_fav = arr_fav_tem;
}
}
for (int i = 0; i < arr_fav.length; i++) {
System.out.println(arr_fav[i]);
}
String[] arr_fav =
{ "1", "2", "3" };
List<String> myList = Arrays.asList(arr_fav);
String currentId = String.valueOf(current_id);
for (int i = 0; i < arr_fav.length; i++)
{
if (arr_fav[i].equals(currentId))
{
myList.remove(i);
}
}
private String[] removeItem(String[] names,
int position) {
ArrayList<String> al_temp=new ArrayList<String>();// temporary ArrayList
for(int i=0;i<names.length;i++)
{
al_temp.add(names[i]);
}
al_temp.remove(position);
names= new String[al_temp.size()];//array cleared with new size
for(int i=0;i<al_temp.size();i++)
{
names[i]=al_temp.get(i);
}
return names;
}
Copy this method:
private static String[] deleteElement(String stringToDelete, String[] array) {
String[] result = new String[array.length];
int index = 0;
ArrayList<String> rm = new ArrayList<String>();
for(int i = 0; i < array.length; i++) {
rm.add(array[i]);
}
for(int i = 0; i < array.length; i++) {
if(array[i].equals(poistettava)) {
index = i;
}
}
rm.remove(index);
result = rm.toArray(new String[rm.size()]);
return result;
}
To delete element:
String[] array = {"1", "2", "3"};
array = deleteElement("3", array);
I'm trying to figure out how to split an array into smaller sections. I have an String array with a bunch of characters. I would like to make a new array that stores the first five of those characters in it's first index, the next five in the next index, etc..
Something like this?
String separator = new String("|");
String [] splits = string.split(separator);
Assuming you have something like this:
String[] myArray = {"12345123", "45123", "45"};
You can split it into an array of five characters like this:
String wholeString="";
for(String s : myArray)
wholeString += s;
int arrayLength = wholeString.length()/5;
if(wholeString.length()%5==0)
arrayLength--;
String[] arrayOfFive = new String[arrayLength];
int counter=0;
String buffer = "";
for(int i=0;i<s.length();i++){
buffer += s.charAt(i);
if(buffer.length()==5){
arrayOfFive[counter] = buffer;
buffer = "";
}
Now, if you don't want to get the whole array string into memory and hold it there, you can do this one character at a time:
String buffer = "";
List<String> stringList = new ArrayList<String>();
for(String s : myArray){
for(int i=0;j<s.length();i++){
buffer += s.charAt(i);
if(buffer.length()==5){
stringList.add(buffer);
buffer = new String();
}
}
}
String[] arrayOfFive = new String[stringList.length()];
stringList.toArray(arrayOfFive);
If you simply have an array of 1-character strings, then you can do it like this:
int arrayLength = myArray.length/5;
if(myArray.length%5==0)
arrayLength--;
String[] arrayOfFive = new String[arrayLength];
for(int i=0;i<myArray.length;i++){
if(i%5==0)
arrayOfFive[i/5] = "";
arrayOfFive[i/5] += myArray[i];
}
If you have a string array containing a single string of length 500, then you can get the string like this:
String myString = myArray[0];
After which you can loop through the characters in the string, breaking it up:
for(int i=0;i<myString.length();i++){
if(i%5==0)
arrayOfFive[i/5] = "";
arrayOfFive[i/5] += myString.charAt(i);
}
List<String> list=new ArrayList<String>();
int chunkSize=5;
for (int i=0; i<strings.size; i++) {
int lastChunk = strings[i].length() % chunkSize;
int chunks=strings[i].length() / chunkSize;
for (int j=0; j<chunks; j++) {
list.add(strings[i].substring(j*chunkSize,j*chunkSize+chunkSize);
}
if (lastChunk > 0) {
list.add(strings[i].substring(chunks*chunkSize, chunks*chunkSize+lastChunk);
}
}
char c[]=str.toCharArray();
int array_length=0;
int start=1;
if(str.length()%5==0)
{
array_length=str.length()/5;
}
else
array_length=str.length()/5 +1;
String s[]=new String[array_length];
for(int i=0;i<array_length;i++)
{
String temp="";
for(int j=start;j<=str.length();j++)
{
if(j%5==0)
{
temp=temp+c[j-1];
start=j+1;
break;
}
else
{
temp=temp+c[j-1];
}
}
s[i]=temp;
}
for(int i=0;i<array_length;i++)
{
System.out.println(s[i]);
}