Split Arraylist of 2 types when printing it

how to split arraylist elements in java
split arraylist java
java split list based on condition
split string into arraylist java
split arraylist into chunks
split java
java how can i split an arraylist in multiple small arraylists
split a list into two halves in java

I have a list composed of Objects Every object is comprised of 2 different values

int number;

String name;

ArrayList<Object> objectList= new ArrayList<Object>();

I have another list composed of Cars Every Car has one value

String type;

ArrayList<Cars> carList= new ArrayList<Cars>();

I want to print half the objectList first then the carList then the other half of the objectList

Like so.. for example:

Output= "number + type + name"

Basically i want the second list to be printed in between the first list.I searched the web but i am not really sure what i should be looking for and how exactly to code this.

Again i am sorry for the bad question.. i am trying to learn coding by myself because i can't afford it.

Thanks in advance.

Just iterate through the first list, and use the index to access the corresponding Object in the second list:

for(int i = 0; i < objectList.size(); i++){
   Object obj = objectList.get(i); 
   Car car    = carList.get(i);

  //Print Here
}

What is the best way to split an array list in Java?, So to split an ArrayList, lets say ogList in to two equal part we will use as below. [​code]List l If you want List type as ArrayList, then you have to cast as this method returns List type. How do I print an attribute from object array in Java? 3) Print ArrayList using Arrays class. You can convert ArrayList to array and use toString method of Arrays class to print elements of an ArrayList. 1. 2. System.out.println("Print Arraylist using Arrays.toString method"); System.out.println( Arrays.toString(aListDays.toArray()) );

You say you want to print half of your list first, then you are able to split the original list by half:

Streams.of(objectList.sublist(0, length/2), 
           carList, 
           objectList.sublist(length/2, length))
.flatMap(Collection::stream)
.forEach(o -> {
    if (o instanceof YourObject) {
       YourObject o1 = (YourObject)o;
       // Print with YourObject
    } else if (o instanceof Cars) {
       // Print car objects
    }
})

Java Split String Into ArrayList Examples, Double To String 2 Decimal Places Examples · String Split Tutorial of the Java String Split, which is a String array, to an ArrayList is to use the  ArrayList is a part of collection framework and is present in java.util package. An ArrayList: ArrayList <E> list = new ArrayList <> (); E here represents an object datatype e.g. Integer. The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

You can very easily achieve this by a for loop. But as per your output format

The size of the objectList and carList must be same for this to work.

Try something like this :

for(int i=0;i<objectList.size();i++){
    String name; int number;
    number = objectList.get(i).number;
    name = objectList.get(i).name;
    String type = carList.get(i).type;
    System.out.Println(name+type+number);
}

As per your comment, if carList is in another class and is private then, you need to set a getter method that takes an index and returns the type Like this :

public class A{
  private ArrayList<Car> carList = new ArrayList();
........ //some codes
  public String getTypeAtIndex(int i){ return carList.get(i).type;  }
    }

Then anywhere you can call

A a;
String type = a.getTypeAtIndex(i);

Java Array of ArrayList, ArrayList of Array, Java Array of ArrayList example, ArrayList of Array in java, java array of list, java l2.add("3"); l2.add("4"); l2.add("5"); List<String>[] arrayOfList = new List[2]; list holds different types of Object arrays, let's print them for (Object[] objArr : list)  The VBA ArrayList is a much better alternative to the built-in VBA Collection. It contains much richer functionality such as sorting, converting to an array, removing all items etc. Check out the quick guide for an overview of what the ArrayList does. The rest of this post provides examples of how to use the ArrayList.

Split a list into two halves in Java, function to split a list into two sublists in Java. public static List[] split(List<String> list). {. // create two empty lists. List<String> first = new ArrayList<String>();. Other Types. Elements in an ArrayList are actually objects. In the examples above, we created elements (objects) of type "String". Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer. For other primitive types, use: Boolean for boolean,

ArrayList to Array Conversion in Java : toArray() Methods , Mathematical · Randomized Algorithms · Greedy Algorithms · Dynamic Programming · Divide and Conquer · Backtracking Following methods can be used for converting ArrayList to Array: Printing array of objects Note: toArray() method returns an array of type Object(Object[]). Method 2: Using T[] toArray(T[] a) Java ArrayList of Object Array. If you are not sure about the type of objects in the array or you want to create an ArrayList of arrays that can hold multiple types, then you can create an ArrayList of an object array. Below is a simple example showing how to create ArrayList of object arrays in java.

How to get sublist of an ArrayList with example, The subList method returns a list therefore to store the sublist in another ArrayList we must need to type cast the returned value in same way as I did in the below  Let us see how to store multiple data types in an java List/ ArrayList, store objects of different data types in an List/ArrayList is pretty simple Please consider disabling your ad blocker for Java4s.com, we won't encourage audio ads, popups or any other annoyances at any point, hope you support us :-) Thank you.

Comments
  • does the two list have the same length?
  • For clarifcation. Do you want something like for example objectList=[obj1,obj2,obj3,...] and carList=[car1,car2,car3,...] and to print "obj1.number car1.type obj1.name" for each?
  • objectList.subList(0, objectList.size() / 2).forEach(System.out::println); carList.forEach(System.out::println); objectList.subList(objectList.size() / 2, objectList.size()).forEach(System.out::println);
  • @Francisco Hey thanks for taking the time to answer. Yes that is the kind of output i am looking for. Also the 2 lists don't have the same length necessarily,
  • @shmosel Can you clarify your code here and how it works? I would really appreciate it. I am not sure how to use it as an example.Thanks aswell!
  • You'll want a flatMap in there.
  • Hey thanks for answering. Is t = i? or is it different?
  • Edited it was a mistake
  • Hey i think that has to be the answer to my problem but what if the carList arraylist is in another class and private? I am stuck at the "String type = carList.get(i).type;" part. I get a cannot find symbol error on carList.
  • If you mark this class private then you can not access it outside by any means. Except as I am about to mention in answer. See edit